【宋红康学习日记20】异常处理

异常分为运行时异常和编译时异常。
一 几种常见的运行时异常(运行时异常可不处理)
1 数组下标越界异常(ArrayIndexOutOfBoundsException)
2 算术异常(ArithmaticException)
3 类型转换异常(ClassCastException)
4 空指针 异常(NullPointerException)

 1     //常见运行时异常
 2     //01 数组越界异常
 3         @Test
 4         public void test1(){
 5             try{
 6             int[] arr=new int[5];
 7             System.out.println(arr[5]);
 8             }catch(Exception e){
 9                 e.printStackTrace();
10                 System.out.println(e.getMessage());
11             }finally{
12                 System.out.println("你真帅");
13             }
14         }
15     //02 算术异常
16         @Test
17         public void test2(){
18             try {
19                 int i=90;
20                 System.out.println(i/0);
21             } catch (Exception e) {
22                 // TODO Auto-generated catch block
23                 e.printStackTrace();
24             }
25         }
26         //03 类型转换异常
27         @Test
28         public void test3(){
29             try {
30                 Object obj=new Date();
31                 String str=(String)obj;
32             } catch (Exception e) {
33                 e.printStackTrace();
34             }
35         }
36         //04 空指针异常
37         @Test
38         public void test4(){
39             try {
40                 String str="SSSS";
41                 str=null;
42                 System.out.println(str.toString());
43             }catch (Exception e) {
44                 e.printStackTrace();
45             }
46         }


二 编译时异常需要处理

 1     //编译时异常
 2         @Test
 3         public void test5(){
 4             FileInputStream fis=null;
 5             try{
 6                 fis=new FileInputStream(new File("hello.txt"));
 7                 int b=0;
 8                 while((b=fis.read())!=-1){
 9                     System.out.println((char)b);
10                 }
11             }catch(FileNotFoundException e){
12                 e.printStackTrace();
13             }catch(IOException e){
14                 e.printStackTrace();
15             }finally{
16                 try {
17                     fis.close();
18                 } catch (IOException e) {
19                     // TODO Auto-generated catch block
20                     e.printStackTrace();
21                 }
22             }
23         }


三 处理异常的模式:抓抛模型
1 抓 有两种方式
(1)try-catch-finally;


(2)throws +异常类型(在方法的声明处)。
2 抛
(1)自动抛:系统自动报错;
(2)手动抛:throw+异常对象。此处的异常类可自己创建。
四 自定义异常类
1 必须继承现有异常类;
2 提供序列号和构造器及重载的构造器。

 1 //06 自定义抛出的 异常类
 2 public class MyException extends RuntimeException{
 3     static final long serialVersionUID = -703489745766939L;
 4     public MyException(){
 5         
 6     }
 7     public MyException(String msg){
 8         super(msg);
 9     }
10 }


五 子类重写父类方法,抛出的异常类型只能是被重写的方法的异常类的子类或异常类型一样。

 1 class AAA{
 2     public void method11() throws Exception{
 3         
 4     }
 5     
 6 }
 7 //重写
 8 class BBB extends AAA{
 9 public void method11() throws IOException{
10         
11     }
12 }
原文地址:https://www.cnblogs.com/noaman/p/4996127.html