捕获和抛出异常
五个关键字
- try
- catch
- finally
- throw
- throws
代码
package com.exception;
public class test {
public static void main(String[] args) {
new test().test(1, 0);
//finally 可以不要finally,假设IO,资源,关闭!
}
//假设这方法中,处理不掉这个异常。方法上抛出异常
public void test(int a,int b) throws ArithmeticException{
if (b==0) { // throw throws
throw new ArithmeticException();//主动抛出异常
}
System.out.println(a/b);
}
}
/*
//假设要捕获多个异常:从小到大!
try { //try监控区域
} catch (Error e) { //catch 捕获异常
System.out.println("Error");
} catch (Exception s){
System.out.println("Exception");
} catch (Throwable t) {
System.out.println("Throwable");
} finally { //处理善后工作
System.out.println("finally");
}
*/