Java篇-异常处理

一 : 常见的异常

  • 空指针异常 : NullpointerExcetion

对象已经为空,却还在发送消息.

public void test5() {
        Person p = new Person();
        p = null;
        System.out.println(p.toString());
    }
  • 数组下标越界的异常 : ArrayIndexOutofBoundsException
    public void test2() {
        int[] i = new int[10];
        System.err.println(i[10]);
  • 算数异常 ArithmeticExceptiom
public void test3() {
        int i = 10;
        System.out.println(i/0);
        
    }
  • 类型转换异常
public void test4() {
        Object obj = new Date();
        String str = (String)obj;
        
    }
  • 编译时异常

如不处理,编译不通过,对于编译时异常来说,必须要显示的进行处理
编译时异常,不是说有异常才处理,而是存在异常的隐患,必须在编译前,提示程序,万一出现异常,如何处理.

public void test6() throws Exception{
        
        FileInputStream fis = new FileInputStream(new File("hello.txt"));
        int b;
        while((b = fis.read())!= -1) {
            System.out.println((char)b);
        
          }
    }

二 : 异常的处理

Java提供异常处理的抓抛模型.
抛: 我们执行代码时,一旦出现异常,就会在异常的代码处生成一个对应的异常类型对象,(自动或者手动)并将此对象抛出,一旦抛出此类型异常对象,那么程序就终止执行,此类异常对象抛给方法的调用者.

抓:抓住上一步跑出来的异常类的对象,怎么样抓住,即为异常的处理方式.如果异常处理了,那么其后的代码继续执行.

  • try catch 处理异常

结构

* try{
 *  //可能出现异常的代码
 * }catch(Exception e1){
 *  //处理方式1
 * }catch(Exception e1){
 *  //处理方式2
 * }finally{
 *      // 一定要执行的代码
 * }

①try 内声明的变量,类似于局部变量,出了try{}语句,就不能被调用,i 的作用范围 只在try{}语句中

public void test3() {
        try {
            int i = 10;
            System.out.println(i/0);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("分母不能为零哦");
        }
        
    }

②finally是可选的,可以不写,如果写了,finally中存放的是一定会被执行的代码,不管try中,catch中是否仍有异常未被处理,以及是否有return语句

public static int method1() {
        try {
        System.out.println("你好");
            //还存在算数异常未处理,但最后 finally 代码还会被执行
         System.out.println(10/0);
            return 1;
            
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return 3;
        }finally {
            System.out.println("最后的执行");
            
        }
    }   

测试

public static void main(String[] args) {
        int i = method1();
        System.out.println(i);
        
        System.out.println("继续哦");
        
    }

结果

你好
/ by zero
最后的执行
3
继续哦

③ catch语句内部是对异常对象的处理
---getMessage();获取打印
---printStackTrace();打印异常的堆栈信息

public void test3() {
        try {
            int i = 10;
            System.out.println(i/0);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        
    }

④可以有多个catch语句,try 中抛出的异常类对象从上往下去匹配catch中的异常类的类型,一旦满足就执行catch中的代码,执行完,就跳出其后的多条catch语句

@Test
        public void test4() {
            try {
                Object obj = new Date();
                String str = (String)obj;
            }catch (ClassCastException e) {
                // TODO: handle exception
                System.out.println("出现类型转换的异常了");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            System.out.println("---的方式发第三方");

        }

执行结果

出现类型转换的异常了
---的方式发第三方

⑤若catch 中多个异常类型是并列关系,谁在上面谁在下都可以.
若catch中多个异常类型是包含关系,须将子类放在父类的上面,进行处理,否则报错

@Test
        public void test6() throws Exception{
            FileInputStream fis = null;
            try {
                 fis = new FileInputStream(new File("hello.txt"));
                int b;
                while((b = fis.read())!= -1) {
                    System.out.println((char)b);
                    
                }
                
            } catch (FileNotFoundException e) {

                //e.printStackTrace();
                //FileNotFoundException 是 IOException 的子类 小的写上面 大的写下面否则出错 
                System.out.println("文件找不到了");
            }catch(IOException e) {
                
                System.out.println(e.getMessage());
                System.out.println(10/0);
                
            }
        }

⑥try - catch 是可以嵌套的

public void test1() {
        Scanner s = new Scanner(System.in);
        try {
            int i = s.nextInt();
            System.out.println(i);
        } catch (InputMismatchException e) {
            // TODO: handle exception
            try {
                System.out.println(10/0);
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            System.out.println("出现类型不匹配的异常了");
        }
        
    }
  • 在方法的声明处,显示的抛出该异常对象的类型

格式

public static void method2() throws  FileNotFoundException,IOException

当在方法内部出现异常的时候,会抛出一个异常类的对象,抛给方法的调用者,异常的对象可以逐层向上抛,直至main中,在向上抛的过程中,还可以用try-catch-finially进行处理.

public class TestException {
    public static void main(String[] args) {
                  //处理抛出的异常
        try {
            TestException.method2();
        } catch (FileNotFoundException e) {
            
            System.out.println(e.getMessage());
        } catch (IOException e) {
            
            e.printStackTrace();
        }
        
    }
    
    //再往上面抛
public static void method2() throws FileNotFoundException,IOException{
        
     method1();
        
    }
    
    //异常抛给方法的调用者
    public static void method1() throws FileNotFoundException,IOException {
        FileInputStream fis = new FileInputStream(new File("TZ.txt"));
        int b;
        while((b = fis.read()) != -1) {
            System.out.println((char)b);

        }
        fis.close();
    }
}
  • throw 手动抛出异常

抛出的异常类型,若是RuntimeException 可以不显示的处理
若是一个Exception,必须要显示的处理.

定义一个Student类 throw Exception

class Student{
    
    int id;
    public void regist(int id) {
        if(id > 0) {
            this.id = id;
        }else {

                      //爆红 因为这个是继承Exception 需要显示处理
            throw new TZException("传入的学号有误");
        }   
    }
}

定义一个Student类 throw RuntimeException

class Student{
    
    int id;
    public void regist(int id){
        if(id > 0) {
            this.id = id;
        }else {
                      //不爆红
            throw new RuntimeException("你传入类型不对");
        }   
    }
}

调用

public class TestStudent {
    public static void main(String[] args) {
        Student s = new Student();
        try {
            s.regist(-12);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
  • 子类重写的父类的方法,其抛出的异常类型只能是被重写的方法的异常类的子类或异常类型一样.

解释 : 编译看左,运行看右(多态),假如 重写的异常大于父类异常,编译时候按照父类的异常,而真正运行的时候则是子类的异常,由于子类的异常大于父类的异常可能会出错,而且Java 也不允许这样做.

public class TestOverride {
    public static void main(String[] args) {
        A a = new B();
        try {
            a.menthod1();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class A {
    public void menthod1() throws IOException{
        
        
    }
}

class B extends A{
    public void menthod1() throws FileNotFoundException{
        
    }
}

三 : 自定义异常

自定义的异常类继承现有的异常类,且提供一个方法号,提供几个重载的构造器.

public class TZException extends Exception{
    static final long serialVersionUID = -7034897190745766939L;
   
    public TZException() {
        
    }
    public  TZException(String msg) {
        super(msg);
        
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容