0
点赞
收藏
分享

微信扫一扫

异常机制学习

月孛星君 2022-03-12 阅读 68

异常机制

异常是指程序运行过程中出现的不期而至的各种状况,如:文件找不到、网络连接失败、非法参数等。
异常发生在程序运行期间,影响正常的程序执行流程。

  1. 检查性异常:用户错误或者问题引起的异常,程序员无法预见,编译时不被忽略
  2. 运行时异常:可能是被程序员避免的异常,可以在编译时忽略
  3. 错误 ERROR:错误不是异常,是脱离程序员控制的问题
    在这里插入图片描述
error

由java虚拟机生成并抛出的,大多数错误与编写者所执行的操作无关

exception

在Exception分支中由一个重要的子类RuntimeException(运行时异常)
这些异常是由程序逻辑错误引起的,程序员应该从逻辑角度尽量避免。
算术异常:ArithmeticException
数组越界异常:ArrayIndexOutOfBoundsException
空指针异常:NullPointerException
找不到类异常:ClassNotFoundException
… 这些异常是不检查异常,程序中可以选择捕获处理,也可不处理。

两者区别:

Error是致命性错误,是程序无法控制和处理的,当出现这些异常时,JVM一般会选择终止线程;
Exception时可以被程序处理的,并且在程序中尽可能去处理这些异常。

抛出异常 捕获异常
关键字:try、catch、finally、throw、throws

public class demo {    
    public static void main(String[] args) {       
        int a = 10;       
        int b = 0;        
        System.out.println(a/b);   
    }
}

在这里插入图片描述

public class demo {    
    public static void main(String[] args) {       
        int a = 10;       
        int b = 0;       
        //监控区域        
        try{            
            System.out.println(a/b);            
            //捕获异常   可以有多个catch 由小到大的写     
        }catch (ArithmeticException e){           
            System.out.println("除数不能为0");
        }catch(Throwable t){
            System.out.println("Throwable");
            //可选的  进行善后工作  终将被执行   
        }finally {            
            System.out.println("finally");       
        }    
   }
}

在这里插入图片描述

idea 快速创建try 选中被监视区域 ctrl+alt+T

throw:是在方法中主动抛出异常

public class demo {    
    public static void main(String[] args) {       
        new demo().test(1,0);   
    }    
    public void test(int a,int b){       
        if(b==0){            
            throw new  ArithmeticException();        
        }    
    }
}

在这里插入图片描述

假设方法处理不了这个异常,在方法上抛出异常
throws:是在方法上抛出异常 需要捕获

public class demo {   
    public static void main(String[] args) {        
        try {            
            new demo().test(1,0);        
        } catch (ArithmeticException e) {            
            e.printStackTrace();        
        }    
    }    
    public void test(int a,int b) throws ArithmeticException{       
            throw new  ArithmeticException();   
          }
}

在这里插入图片描述

自定义异常

构建一个构造函数 重写toString方法

//创建自己的异常
public class MyException extends Exception{    
//传值大于100 出现异常    
    private int value;   
    public MyException(int value) {        
        this.value = value;   
    }    
    @Override   
    public String toString() {        
    return "MyException{" 
    +   "value=" + value +  '}';    
    }
}
public class test {    
    public static void main(String[] args)  {        
        try {            
            get(105);       
        } catch (MyException e) {           
            System.out.println(e);       
        }    
    }    
    public static void get(int a) throws MyException {        
        System.out.println("传递的参数为"+a);        
        if(a>100){            
            throw new MyException(a);        
        }        
        System.out.println("ok");    
    }
}

在这里插入图片描述

举报

相关推荐

0 条评论