/*
异常的处理方式2:抛出处理. (throw / throws)
抛出处理注意的细节:
1.如果一个方法内部抛出了一个异常对象,那麽必须在方法上声明抛出
2.如果调用了一个声明抛出异常类型的方法,那么调用者必须要进行处理,否则编译报错
3.一个方法遇到了throw关键字,那麽该方法会马上停止执行。
4.在一种情况下只能抛出一种异常对象。
*/
class Demo
{
public static void main(String[] args) throws Exception
{
//System.out.println("Hello World!");
try{
div(4,0,null);
}catch(Exception e){
System.out.println("过度杠死掉了哈哈哈。。。");
}
}
public static void div(int a , int b , int[] arr) throws Exception{
if(b==0){
throw new Exception();//抛出一个异常
}else if (arr==null){
throw new NullPointerException();
}
int c = a/b;
System.out.println("结果是:"+c);
}
}
异常处理方式——抛出处理
最新推荐文章于 2023-08-09 21:35:39 发布