1、三种类型异常:checked exception、unchecked exception(runtime exception)、error
2、五个关键字:try、catch、finally、throw、throws
3、使用try…finally…. | try….catch….finally
那什么时候使用try-finally,什么时候使用try-catch-finally呢?这取决于方法本身是否能够处理try中出现的异常。如果自己可以处理,那么直接catch住,不用抛给方法的调用者;如果自己不知道怎么处理,就应该将异常向外抛,能够让调用者知道发生了异常
4、异常捕获:从上至下进行,并且只会捕获一次,并且通用的异常必须在下面
public class TestMultipleCatchBlock{
public static void main(String args[]){
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e){System.out.println("task1 is completed");}
catch(ArrayIndexOutOfBoundsException e){System.out.println("task 2 completed");}
catch(Exception e){System.out.println("common task completed");}
System.out.println("rest of the code...");
}
}
5、异常抛出(
throw),在方法内执行
抛出示例:throw new IOException(“throw exception”);
可以抛出checked exception/unchecked exception,但一般用来抛出custom exception(checked exception),
例如对外提供dubbo api接口,api方法声明throws exception,调用者传递参数进行调用,如果调用者提供不合法参数,则抛出异常,让调用者知晓
6、异常声明(throws):void m() throws ArithmeticException{ }
7、throw与throws区别
8、异常传播(propagation):调用堆栈从上至下,直到被捕获处理
9、Final、finally、finallize区别