java抛出异常心得

一、异常的抛出

1、定义 : 一个方法不处理这个异常,而是调用层次向上传递,谁调用这个方法,这个异常就由谁来处理。

2、throw : 将产生的异常抛出(强调的是动作),抛出的既可以是异常的引用,也可以是异常对象。(位置: 方法体内)

3、throws : 如果一个方法可能会出现异常,但没有能力处理这种异常,可以在方法声明处用throws子句来声明抛出异常。用它修饰的方法向调用者表明该方法可能会抛出异常(可以是一种类型,也可以是多种类型,用逗号隔开)(位置: 写在方法名 或方法名列表之后 ,在方法体之前。)

注意 : 调用可能会抛出异常的方法,必须添加try-catch代码块尝试去捕获异常 或者 添加throws 声明 来将异常 抛出给更上一层的调用者进行处理,这里需要注意一个细节:新的异常包含原始异常的所有信息,根据这个我们可以去追溯最初异常发生的位置,

4、简单使用

// 定义一个方法,抛出 数组越界和算术异常(多个异常 用 “,” 隔开)
public void Test1(int x) throws ArrayIndexOutOfBoundsException,ArithmeticException{

System.out.println(x);

if(x == 0){

    System.out.println("没有异常");
    return;
}

//数据越界异常
else if (x == 1){

    int[] a = new int[3];
     a[3] = 5;
}

//算术异常
else if (x == 2){

    int i = 0;
    int j = 5/0;
}

}

在main方法中调用

public static void main(String[] args) {

    //创建对象
    ExceptionInital object = new ExceptionInital();

    // 调用会抛出异常的方法,用try-catch块
    try{

        object.Test1(0);

    }catch(Exception e){

        System.out.println(e);
    }

    // 数组越界异常
    try{

        object.Test1(1);
    }catch (ArrayIndexOutOfBoundsException e) {

        System.out.println("数组越界异常:"+e);
    }


    // 算术异常
    try{

        object.Test1(2);

    }catch(ArithmeticException e){

        System.out.println("算术异常:"+e);
    }


    //使用 throw 抛出异常(可以抛出异常对象,也可以抛出异常对象的引用)
    try{

        ArrayIndexOutOfBoundsException  exception = new ArrayIndexOutOfBoundsException();

        throw exception;//new ArrayIndexOutOfBoundsException();

    }catch(ArrayIndexOutOfBoundsException e){

        System.out.println("thorw抛出异常:"+e);
    }

}

总结下 throw 和throws 关键字的区别

1、写法上 : throw 在方法体内使用,throws 函数名后或者参数列表后方法体前
2、意义 : throw 强调动作,而throws 表示一种倾向、可能但不一定实际发生
3、throws 后面跟的是异常类,可以一个,可以多个,多个用逗号隔开。throw 后跟的是异常对象,或者异常对象的引用。
4、throws 用户抛出异常,当在当前方法中抛出异常后,当前方法执行结束(throws 后,如果有finally语句的话,会执行到finally语句后再结束。)。可以理解成return一样。

二、自定义异常

前面所讲的异常,都是系统自带的,系统自己处理,但是很多时候项目会出现特有问题,而这些问题并未被java所描述并封装成对象,所以对于这些特有的问题可以按照java的对问题封装的思想,将特有的问题进行自定义异常封装。在Java中要想创建自定义异常,需要继承Throwable或者他的子类Exception。

class 自定义异常类 extends 异常类型(Exception){

// 因为父类已经把异常信息的操作都完成了,所在子类只要在构造时,将异常信息传递给父类通过super 语句即可。
// 重写 有参 和 无参 构造方法
}

例如:

public class CustomException extends Exception {

//无参构造方法
public CustomException(){

    super();
}

//有参的构造方法
public CustomException(String message){
    super(message);

}

// 用指定的详细信息和原因构造一个新的异常
public CustomException(String message, Throwable cause){

    super(message,cause);
}

//用指定原因构造一个新的异常
 public CustomException(Throwable cause) {

     super(cause);
 }

}

// 备注: 这些方法怎么来的? 重写父类Exception的方法,那么如何查看Exception具有哪些API,快捷键:选中Exception, command+单击。windows系统 :选中Exception, control+单击。

自定义异常的使用例子:
自定义test1()方法,抛出 “我喝酒了”的异常信息,test2()方法调用test1()方法,并将异常包装成RuntimeException类型的异常,继续抛出,在main方法中调用test2()方法,并尝试捕获异常

public void test2() {

    try{

        test1();

    }catch (CustomException e){

       RuntimeException exception  =  new RuntimeException(e);
       //exception.initCause(cause)
       throw  exception;
    }

}

public void test1() throws CustomException{

    throw new CustomException("我喝酒了");
}

// main方法
public static void main(String[] args) {

    CustomExceptionInital object =  new  CustomExceptionInital();

    //try{

        object.test2();

    //}catch(Exception e){

    //e.printStackTrace();

    //}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值