异常处理

异常不同于错误,是需要进行处理的,常见的处理方式是throws或者try/catch块

  1 package javabase;
  2 
  3 public class Exceptionclass {
  4 /**
  5  * 异常体系----java.lang.Throwable
  6  *                             |----Error 通常情况下,不编写针对性的改错语句,由jvm发生的,需要对程序进行修改
  7  *                             |----Exception 异常,需要有针对性的异常处理方式
  8  * 初识异常
  9  * 
 10  * class Demo{
 11  * static int div(int a, int b){
 12  * return a/b;
 13  * }
 14  * }
 15  * public static void main(String[] args){
 16  *     try{
 17  *      int x = Demo.div(5,0);
 18  *      System.out.println(x);
 19  *     }
 20  *     catch(Exception e){
 21  *         System.out.println(e.getMessage());
 22  *         System.out.println(e.toString());
 23  *         e.printStackTrack();
 24  *     }
 25  *     finally{
 26  *         System.exit(0);
 27  *     }
 28  * }
 29  * 
 30  * 异常的处理涉及到2个关键字throws和throw,一个异常处理块try/catch/finally
 31  * 
 32  * throws 和 throw的区别:
 33  *     --throws使用在函数上,抛出异常类,当抛出多个时,用逗号隔开,并且对应多个处理的catch代码块
 34  *  --throw使用在函数内,抛出异常对象,必须在函数上进行声明,否则编译失败。一种情况除外,RuntimeException.
 35  *     因为RuntimeException及其子类是运行异常,并不参与编译。
 36  * 
 37  * 异常处理:
 38  * --功能内部如果出现了异常,能够处理的使用try,不能处理的使用throws,让调用者处理。
 39  * 
 40  * 自定义异常:
 41  * --自定义一个子类继承Throwable类,因为只有这个类具有可抛性
 42  * --在函数上声明,并在函数体内抛出。
 43  * 
 44  * 子类覆盖父类异常
 45  * --父类抛出异常,子类继承时可以不抛或者抛出父类异常的子类
 46  * --父类不抛出异常,子类不能抛出异常,如果存在异常情况,在函数类处理,如果处理不了,可以通过throw new RuntimeException()
 47  * 
 48  * 举例子说明:
 49  * --RuntimeException的使用
 50  * class  Person{
 51  *     public void checkName(String name){
 52  *         if(name.equals("Lisi")) //此时会抛出空指针异常,常用的处理是(1)if("Lisi".equals(name)) (2)if(name==null) throw new NullPointException,不需要在函数上进行声明throws NullPointException
 53  *             System.out.print("Yes");
 54  *         else System.out.print("No");
 55  *     }
 56  * public static void main(String[] args){
 57  *     Person person = new Person();
 58  *     person.checkName(null);
 59  * }
 60  * 
 61  * --try/catch/finally模块的使用
 62  *     try{
 63  *         //code that does throw exceptions
 64  *     }
 65  *     catch(My Exception1){
 66  *         //code to process exceptions
 67  *     }
 68  *  catch(My Exception2){
 69  *         //code to process exceptions
 70  *     }
 71  *     finally{
 72  *         //code to execute after the try block
 73  *     }
 74  *     执行从try代码块的开头执行,执行会从发生异常的位置中断,转移到与异常类型对应的catch代码块的开头,catch执行完会执行finally
 75  *     ,如果在catch和finally代码块中没有返回语句,那么会继续执行finally后的代码。
 76  * 
 77  * class Test{
 78  *     public static int div(int[] array, int index){
 79  *         try{
 80  *             System.out.println("First try block in div() entered");
 81  *             array[index+2] = array[index]/array[index+1];
 82  *            System.out.println("Code at end of first block in div()");
 83  *             return array[index+2];
 84  *         }
 85  *         catch(ArithmeticException e){
 86  *             System.err.println("ArithmeticException in div()");
 87  * 
 88  *         }
 89  *         catch(ArrayIndexOutOfBoundsException e){
 90  *             System.err.println("ArrayIndexOutOfBoundsException in div()");
 91  *         }
 92  *         finally{
 93  *             System.err.println("finally in div()");
 94  *         }
 95  *         System.out.println("Executing code after my block in div()");
 96  *         return array[index+2];
 97  *     }
 98  *     public static void main(String[] args){
 99  *         int[] x={10,5,0};
100  *         try{
101  *             System.out.println("First try block in main() entered");
102  *             System.out.println("result= "+div(x,0));
103  *             x[1]=0;
104  *             System.out.println("result= "+div(x,0));
105  *             x[1]=1;
106  *             System.out.println("result= "+div(x,0));
107  *         }
108  *         catch(ArithmeticException e){
109  *             System.err.println("ArithmeticException in main()");
110  *         }
111  *      catch(ArrayIndexOutOfBoundsException e){
112  *             System.err.println("ArrayIndexOutOfBoundsException in main()");
113  *         }
114  *         finally{
115  *             System.out.println("finally in main()");
116  *         }
117  *         System.out.println("Executing code after my block in main()");
118  *     }
119  * }
120  * 
121  * 写出执行结果:
122  * First try block in main() entered
123  * First try block in div() entered
124  * Code at end of first block in div()
125  * result= 2
126  * 
127  * First try block in div() entered
128  * ArithmeticException in div()
129  * finally in div()
130  * Executing code after my block in div()
131  * result= 2
132  * 
133  * First try block in div() entered
134  * ArrayIndexOutOfBoundsException in div()
135  * finally in div()
136  * Executing code after my block in div()
137  * ArrayIndexOutOfBoundsException in main()
138  * finally in main()
139  * Executing code after my block in main()
140  * 
141  * --自定义异常类的使用不是很多,写个简单的例子
142  * class NegativeException extends Exception{
143  *     private String msg;
144  *     NegativeException(String msg){
145  *         super(msg);
146  *     }
147  *     public String getMsg(){
148  *         return msg;
149  *     }
150  * }
151  *     public static class Demo{
152  *     public static int div(int a, int b) throws NegativeException,ArithmeticException{
153  *         if(b<0) throw new NegativeException("出现负数啦!");
154  *         int num =a/b;
155  *         return num;
156  *     }
157  * public static void main(String[] args){
158  *         try{
159  *             int x = Demo.div(5,-1);
160  *             System.out.println("x="+x);
161  *         }
162  *         catch(NegativeException e)
163  *         {
164  *             System.out.println(e.toString());
165  *         }
166  *         catch(ArithmeticException e){
167  *             System.out.println("除0啦!");
168  *         }
169  * }
170  * }
171  */
172 }

 

posted @ 2013-05-09 11:46  hustsing  阅读(120)  评论(0)    收藏  举报