在Java中有三种抛出异常方式
1.系统自己抛
public static void main(String[] args) {
int a = 5, b =0;
System.out.println(5/b);
//function();
}
系统自己抛出:
Exception in thread “main” java.lang.ArithmeticException: / by zero
at test.ExceptionTest.main(ExceptionTest.java:62)
2.throw(异常对象)
用于程序出现某种逻辑时程序员主动抛出某种特定的的异常,记住是程序员主动 抛的
public void WithAop() throws Exception{
System.out.println(“拦截调用”);
if(name.trim().length()==0)
throw new AccountException(“name属性为空”);
}
3.throws
指的是方法可能抛出异常的声明,当某个方法可能会抛出某种异常时用于throws 声明可能抛出的异常,然后交给上层调用它的方法程序处理。
对比
throw
throws
位置 函数体 方法函数头
执行 有异常才会执行 一定执行
作者:猿类是你
来源:CSDN
原文:https://blog.csdn.net/qq_38903213/article/details/80396836
版权声明:本文为博主原创文章,转载请附上博文链接!