异常
package com.ljh.oop.Demo12;
public class Test {
public static void main(String[] args) {
int a = 2;
int b = 0;
try {//try监控区域
System.out.println(a/b);
}catch (Error e){
}catch (Exception e){
e.printStackTrace();//打印错误的栈信息
}catch (Throwable e){//catch(想要捕获的异常类型)捕获异常
System.out.println("程序出现异常,变量b不能为0");
}finally {//处理善后工作
System.out.println("finally");
}
//ctrl+alt+t
}
}
package com.ljh.oop.Exception;
//自定义的异常类
public class MyException extends Exception{
//传递数字
private int detail;
public MyException(int a) {
this.detail = a;
}
//打印异常信息
@Override
public String toString() {
return "MyException{" +
"detail=" + detail +
'}';
}
}
package com.ljh.oop.Exception;
public class Test {
//可能会存在异常的方法
static void test(int a) throws MyException{
System.out.println();
if(a>10){
throw new MyException(a);
}
}
public static void main(String[] args) {
try {
test(11);
} catch (MyException e) {
e.printStackTrace();
} finally {
}
}
}