再写java的时候,
使用debug模式的时候
发现 自己写的try catch语句不能捕获到异常
而是直接跳到finally里面
然后跟踪路径 发现自己跑到其他系统的异常块中了
究其原因
是因为异常可能不属于Excepttion的子类
如下
- public static void main(String[] args) throws Throwable {
-
- try{
- throw new Throwable(){
-
- };
- }
- catch(Exception e){
- System.out.println("err");
- }
- finally{
- System.out.println("finally");
- }
-
- }
则不能捕获异常直接跳进finally
只输出finally
可以这样写就能捕获异常
- public static void main(String[] args) throws Throwable {
-
- try{
- throw new Throwable(){
- };
- }
- catch(Exception e){
- System.out.println("err");
- }
- catch(Throwable t){
- System.out.println("<span style="font-family: Arial, Helvetica, sans-serif;">Throwable </span>");
- }
- finally{
- System.out.println("finally");
- }
-
- }
这样就可以捕获到
输出throwable finally
异常的分类是
![](https://img-blog.csdn.net/20161008202331113?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)