Class Annoyance extends Exception {}class Sneeze extends annoyance {}class Human {public static void main (string[] args) t Hrows Exception {try {try {throw new sneeze ()} catch (Annoyance a) {System.out.println ("caught annoyance"); throw A;}} C Atch (sneeze s) {System.out.println ("caught Sneeze"); return;} finally {System.out.println ("Hello world!");}}}
Today I see a piece of code, what is the output of the content?
Let's not give an answer, think about the process and sequence of abnormal capture that we usually write code in, such as a piece of code:
Try {throw new FileNotFoundException ();} catch (IOException e) {}
We can often see this code, that is, using the parent class to catch the exception of the subclass, of course, all exceptions are inherited exception, then why not exception replace all the specific exception classes? This is to take into account our understanding of the expected anomalies in the code, that is, what kind of exception the code will produce, the programmer should know, if all use exception reference to receive, everyone is confused, how to handle it will become difficult to start. But if in the catch code block do not intend to do any processing, only capture, then the use of exception also does not matter, but it is the production environment, such circumstances should not be allowed to exist, this look at the JDK source code will know, catch the exception must be given the prompt information.
The above code proves that we can accept the subclass's exception object with a reference to the parent class, so the question is, what is the result of the top code execution? The answer is:
Caught Annoyancecaught Sneezehello world!
Believe that the first row and the third row everyone has no doubt, the key is the second line, should come out? Does the subclass catch the exception of the parent class?
After the addition of the breakpoint, we found that, despite
This sentence uses a reference to the parent class, but is actually the object of the subclass, which is the classic representation of polymorphism in Java. In
catch (Sneeze s)
Of course you can catch the exception that you throw out.
To prove that the subclass is inherently unable to catch the exception of the parent class, we continue to experiment:
Try {throw new annoyance (),} catch (Sneeze s) {System.out.println ("caught Sneeze"), return;} finally {System.out.println ( "Hello world!");}
As you can see, throws the exception of the parent class, using the subclass catch, which can be compiled at this time, but at runtime?
Hello world! Exception in thread "main" Com.xq.exceptions.Annoyanceat Com.xq.exceptions.Human.main (exceptiontest.java:14)
You can see that there is a problem, that is, the parent class throw out the exception, the subclass is not captured, continue to prove:
Try {throw new annoyance ()} catch (Sneeze s) {System.out.println ("caught Sneeze"); return;} catch (Exception e) {System.ou T.println ("Caught Exception"); return;} finally {System.out.println ("Hello world!");}
Now that the subclass is not capturing, use exception to see the results as follows:
Caught Exception
Hello world!
See this, everyone must understand, do not say more!
A little insight into Java exception capture