转载于:https://blog.csdn.net/salerzhang/article/details/46581457
-
class Annoyance extends Exception {}
-
class Sneeze extends Annoyance {}
-
-
class Human {
-
-
public static void main(String[] args)
-
throws Exception {
-
try {
-
try {
-
throw new Sneeze();
-
}
-
catch ( Annoyance a ) {
-
System.out.println( "Caught Annoyance");
-
throw a;
-
}
-
}
-
catch ( Sneeze s ) {
-
System.out.println( "Caught Sneeze");
-
return ;
-
}
-
finally {
-
System.out.println( "Hello World!");
-
}
-
}
-
}
今天看到这么一段代码,请问输出的内容是什么?
先别给出答案,思考一下平时我们写代码的过程中,异常捕获的过程和顺序,比如有这么一段代码:
-
try {
-
throw new FileNotFoundException();
-
} catch (IOException e) {
-
-
}
以上的代码证明了,我们可以用父类的引用接受子类的异常对象,那么问题来了,最上面的代码执行结果是什么呢?答案是:
-
Caught Annoyance
-
Caught Sneeze
-
Hello World!
经过断点的加入,我们发现,尽管
catch ( Annoyance a )
这一句使用的是父类的引用,但实际上是子类的对象,这是java中多态的经典表现。在
catch ( Sneeze s )
的时候当然可以捕获到自己抛出来的异常了。
为了证明,子类从本质上无法捕获父类的异常,我们继续做个试验:
-
try {
-
-
throw new Annoyance();
-
} catch (Sneeze s) {
-
System.out.println( "Caught Sneeze");
-
return;
-
} finally {
-
System.out.println( "Hello World!");
-
}
可以看到,抛出了父类的异常,使用子类catch,这时候可以通过编译,但运行时呢?
-
Hello World!
-
Exception in thread "main" com.xq.exceptions.Annoyance
-
at com.xq.exceptions.Human.main(ExceptionTest.java: 14)
-
try {
-
-
throw new Annoyance();
-
} catch (Sneeze s) {
-
System.out.println( "Caught Sneeze");
-
return;
-
} catch (Exception e) {
-
System.out.println( "Caught Exception");
-
return;
-
} finally {
-
System.out.println( "Hello World!");
-
}
既然子类捕获不了,那就使用Exception,可以看到结果如下:
Caught Exception
Hello World!
看到这样,大家肯定都明白了,不用多说了!
-
class Annoyance extends Exception {}
-
class Sneeze extends Annoyance {}
-
-
class Human {
-
-
public static void main(String[] args)
-
throws Exception {
-
try {
-
try {
-
throw new Sneeze();
-
}
-
catch ( Annoyance a ) {
-
System.out.println( "Caught Annoyance");
-
throw a;
-
}
-
}
-
catch ( Sneeze s ) {
-
System.out.println( "Caught Sneeze");
-
return ;
-
}
-
finally {
-
System.out.println( "Hello World!");
-
}
-
}
-
}
今天看到这么一段代码,请问输出的内容是什么?
先别给出答案,思考一下平时我们写代码的过程中,异常捕获的过程和顺序,比如有这么一段代码:
-
try {
-
throw new FileNotFoundException();
-
} catch (IOException e) {
-
-
}
以上的代码证明了,我们可以用父类的引用接受子类的异常对象,那么问题来了,最上面的代码执行结果是什么呢?答案是:
-
Caught Annoyance
-
Caught Sneeze
-
Hello World!
经过断点的加入,我们发现,尽管
catch ( Annoyance a )
这一句使用的是父类的引用,但实际上是子类的对象,这是java中多态的经典表现。在
catch ( Sneeze s )
的时候当然可以捕获到自己抛出来的异常了。
为了证明,子类从本质上无法捕获父类的异常,我们继续做个试验:
-
try {
-
-
throw new Annoyance();
-
} catch (Sneeze s) {
-
System.out.println( "Caught Sneeze");
-
return;
-
} finally {
-
System.out.println( "Hello World!");
-
}
可以看到,抛出了父类的异常,使用子类catch,这时候可以通过编译,但运行时呢?
-
Hello World!
-
Exception in thread "main" com.xq.exceptions.Annoyance
-
at com.xq.exceptions.Human.main(ExceptionTest.java: 14)
-
try {
-
-
throw new Annoyance();
-
} catch (Sneeze s) {
-
System.out.println( "Caught Sneeze");
-
return;
-
} catch (Exception e) {
-
System.out.println( "Caught Exception");
-
return;
-
} finally {
-
System.out.println( "Hello World!");
-
}
既然子类捕获不了,那就使用Exception,可以看到结果如下:
Caught Exception
Hello World!
看到这样,大家肯定都明白了,不用多说了!