packagecom.exception.dome01;publicclassTest{publicstaticvoidmain(String[] args){int a =1;int b =0;try{//try监控区域System.out.println(a/b);}catch(Error e){//捕获异常System.out.println("Error");}catch(Exception e){//捕获异常System.out.println("Exception");}catch(Throwable e){//捕获异常System.out.println("Throwable");}finally{//善后工作System.out.println("finally");//finally 可以不要finally}}}
Test2
packagecom.exception.dome01;publicclassTest2{publicstaticvoidmain(String[] args){int a =1;int b =0;//new Test().test(1,0); 这我写了报错不知道为什么}publicvoidtest(int a,int b)throwsArithmeticException{if(b==0){//throw throwsthrownewArithmeticException();//主动的抛出异常,一般在方法中使用}}}/*
int a = 1;
int b = 0;
//快捷键 Ctrl+Alt+T 选择你要的代码
try {
if (b==0){ //terow terows
throw new ArithmeticException();//主动抛出异常
}
System.out.println(a/b);
} catch (Exception e) {
e.printStackTrace();//打印错误的站信息
} finally {
}
*/
Error和ExceptionTestpackage com.exception.dome01;public class Test { public static void main(String[] args) { int a = 1; int b = 0; try {//try监控区域 System.out.println(a/b); }catch (Error e){//捕获异常