Unchecked Exception:(编译器不会辅助检查),包括Error子类和RuntimeException子类
Error的子类,可以不用处理
RuntimeException子类,程序必须处理,以预防为主。编译器不会辅助检查
Checked Exception(非RuntimeException的Exception的子类)
程序必须处理,以发生后处理为主,编译器会辅助检查
一种保护代码正常运行的机制
异常结构
tips:try必须有,catch和finally至少要有一个
—要实现的功能代码
—当try发生异常,将执行catch代码
—当try或catch执行结束后,必须要执行finally
public class MyException extends Exception {
private String returnCode ; //异常对应的返回码
private String returnMsg; //异常对应的描述信息
public MyException() {
super();
}
public MyException(String returnMsg) {
super(returnMsg);
this.returnMsg = returnMsg;
}
public MyException(String returnCode, String returnMsg) {
super();
this.returnCode = returnCode;
this.returnMsg = returnMsg;
}
public String getReturnCode() {
return returnCode;
}
public String getreturnMsg() {
return returnMsg;
}
}
public class MyExceptionTest {
public static void testException() throws MyException {
throw new MyException("10001", "The reason of myException");
//方法内部程序中,抛出异常 throw
//方法头部声明中,抛出异常 throws
}
public static void main(String[] args) {
//MyExceptionTest.testException();
try {
MyExceptionTest.testException();
} catch (MyException e) {
e.printStackTrace();
System.out.println("returnCode:"+e.getReturnCode());
System.out.println("returnMsg:"+e.getreturnMsg());
}
}
}