/*
异常的处理方式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);
}
}
![](https://img-blog.csdnimg.cn/2018112519515884.png)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)