首页 > 其他分享 >(抛出)异常oppdemo12

(抛出)异常oppdemo12

时间:2022-10-13 10:44:44浏览次数:42  
标签:抛出 ArithmeticException System catch println 异常 oppdemo12 out

import java.sql.SQLOutput;

public class Test {
public static void main(String[] args) {

//方法中处理的了异常,new Test().test(1,0);一行代码就够了
try {
new Test().test(1,0);
} catch (ArithmeticException e) {
throw new RuntimeException(e);
}

/*
int a = 2 ;
int b = 0 ;

//假设要捕获多个异常,需要从小到大(Error/Exception-->Throwable)
try {//try监控区域
System.out.println(a/b);
}catch (Error e){//catch(想要捕获的异常类型)捕获异常
System.out.println("Error");
} catch (Exception e){
System.out.println("Exception");
}catch (Throwable e){
System.out.println("Throwable");
} finally{//处理善后工作,添加finally语句块去释放占用的资源
System.out.println("finally");
}
*/

}
//若假设方法中处理不了这个异常。在方法上抛出异常throws ArithmeticException
public void test(int a,int b) throws ArithmeticException{
if (b==0){//throw
throw new ArithmeticException();//主动抛出异常,一般在方法中使用
}
System.out.println(a/b);
}
}

标签:抛出,ArithmeticException,System,catch,println,异常,oppdemo12,out
From: https://www.cnblogs.com/123jgh/p/16787334.html

相关文章