javaSE-06Error和Exeption
Error和Exeption
Error是灾难性的致命的错误,程序可以不可以处理,会终止线程
Exeption通常是程序可以处理的
异常体系图

异常处理机制
关键字
- try 监视区域
- catch 捕获异常
- finally 处理善后工作
- throw 抛出异常
- throws 抛出方法异常
快捷键
- 选择要监视的区域
- ctrl+alt+T
- try /catch /finally
自定义异常
自定义一个数值大于10就报错的异常代码如下:
public class MYException extends Exception {
private int datail;
public MYException(int a){
this.datail=a;
}
//重写了Exception里的toString()方法
//toString:异常的打印信息
@Override
public String toString() {
return "MYException{" +
+ datail +
'}';
}
}
public class Test {
static void test(int a)throws MYException{
//如果a大于10就抛出异常
if (a>10){
throw new MYException(a);
}
System.out.println("ok");
}
public static void main(String[] args) {
try {
test(11);
} catch (MYException e) {
System.out.println(e);
}
}
}

浙公网安备 33010602011771号