异常处理的方式

处理异常的方式:
对于编译期异常处理有两种不同的处理方式。
(1) 使用try{ } catch { } finally 语句块处理它。
(2)在函数签名中使用throws 声明交给函数调用者去解决。

try catch finally 块是异常的捕获,其本质是判断,基本语法如下:

try{
    可能出现异常的代码(包括不会出现异常的代码)
   } catch( Exception e) { // 括号里面接收try代码块中出现的异常类型
    如果出现异常时的处理代码
   
   }finally {
    不管代码是正常执行还是出现异常需要处理,finally代码块中的代码最终都会执行
}

自定义异常
格式:
(1)自定义一个编译器异常:自定义类并继承java.lang.Exception。
(2)自定义一个运行时期的异常类:自定义类,并继承于java.lang.RuntimeException。

public class 自定义异常类 extends java.langException{
}

使用时需要输出异常信息,这需要调用父类的构造方法。

public class PersonException extends Exception{
    private static final long serialVersionUID = 1L;
    
    public PersonException(){
        super();
    }
    
    
    public PersonException(String message){
        super(message);
    }
}

使用自定义异常:

public void savePerson(Person person) throws PesonException {
   
   if( null = = person.getPId()(){
      throw new PersonException("没有Person编号,请检查");
   }
}

注意:

(1)自定义异常类一般是Exception结尾的。

(2) 自定义异常类,必须继承Exception或者RuntimeException。

1)继承Exception,那么自定义的异常类就是一个编译期异常,如果方法内部抛出了编译期异常,就必须处理这个异常,要么throws,要么try...catch。

2)继承RuntimeException,那么自定义的异常类就是一个运行期异常,无法处理,交给虚拟机处理(中断处理)。

Dome01:

public class Dome01 {
    public static void main(String[] args) {
        int divisor  = 10 ;
        int dividend = 0 ;
        // System.out.println(divisor/dividend);  // ArithmeticException  算术异常
        try {
            System.out.println(divisor / dividend);  //ArithmeticException 算术异常
        }catch (Exception e ) {
            e.printStackTrace();
            System.out.println("捕获到一个异常");
        }finally {
            System.out.println("不管如何都会执行这里的代码");
        }
        System.out.println("哈哈哈哈哈哈");

    }
}

Dome02:

public class Dome02 {
    public static void main(String[] args) {
        int[] a = new int[2];
        Scanner scanner = new Scanner(System.in);
        try {
            int i = scanner.nextInt();
            int j = scanner.nextInt();
            a[0] =i;
            a[2] = j;
            System.out.println( a[0] / a[2]);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("数据越界异常");
        } catch (NumberFormatException e) {
            System.out.println("数据格式不正确异常");
        } catch (ArithmeticException e) {
            System.out.println("算数异常");
        }
    }
}

Dome03:

public class Dome03 {
    public static void main(String[] args) {
        int[] a = new int[2];
        Scanner scanner = new Scanner(System.in);
        try {
            int i = scanner.nextInt();
            int j = scanner.nextInt();
            a[0] = i;
            a[2] = j;
            System.out.println( a[0] / a[2]);
            // Array Index OutOf Bounds Exception  数组 索引 超出 边界 异常
            // Input Mismatch Exception  输入 不匹配 异常
            // Arithmetic Exception  数学数字 异常
        } catch (ArrayIndexOutOfBoundsException | InputMismatchException | ArithmeticException e) {
            System.out.println("数据越界异常");
            System.out.println("数据格式不正确异常");
            System.out.println("算数异常");
            System.out.println("以上异常中的一个");
        }

    }
}

Dome04:

public class Dome04 {
    public static void main(String[] args) throws Exception{       //继续向上声明,不处理
        /*try {
            steSex("双性人");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非男非女");
        }*/
        steSex("afwarf");
    }
    public static void steSex(String sex) throws Exception{  //声明异常
        if (!(sex.equals("男")|| sex.equals("女"))){
            throw new Exception("处理不了的异常,扔出去");  //抛出异常
        }
    }
}

Dome05

public class Dome05 {
    public static void main(String[] args){
        try {
            steSex("双性人");
        }catch (Exception e){
            System.out.println("调用者说处理过了");
        }
    }
    public static void steSex(String sex) throws SexException{  //声明异常
        if (!(sex.equals("男")|| sex.equals("女"))){
            throw new SexException("发现一个不对劲的");  //抛出异常
        }
    }
}

SexException:

public class SexException extends Exception {
    public SexException(){
    }
    public SexException(String message){
        System.out.println(message);
        System.out.println("自定义的异常,知道非男非女,但是没办法处理");
        System.out.println("..........");
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容