Look at the abnormal mechanism of the time feel caught the essence of the mechanism, so to say, for beginners should have some help in Java exception mechanism
- From the mechanism consists of " Generate exception" "Throw Exception" "catch Exception" "Exception Handling"
- The form is divided into four kinds:
- The system produces an exception when a bug occurs at run time. The most classic way to throw exceptions
- Include "Generate exception""Throw exception "
- Throw The form is "first yourself new Exception object, then throw the exception object", anywhere in the code can be manually generated throw
- Include "Generate exception""Throw exception "
- Try...catch...finally. The most classic way to catch anomalies and handle them
- Include catch exception,exception handling
- Throws Its function is to forward the exception to the upper layer, it is added to a method, when the method is called, the method may produce an exception, and the exception from the method to send out to the method called this method
- Include "catch exception""throw exception "
- Can be found to "abnormal handling" of only one, can "produce abnormal" there are two, can "catch the exception" of two, can "throw an exception" but there are three
- So the usual forms of application are:
- The first use of the "try...catch...finally" or "throws" these two can "catch the exception" of the "exception", "Throw exception" of the "run-time bug system generated anomalies" or "throw"
- and then where
- The "try...catch...finally" with "Exception handling" or "throw" because it is a "catch exception" with "abnormal processing", so the exception can be handled, and the exception processing ends here.
- The "throws", which is a "bug system at run time" or "throw" because it is "catch exception" without "Exception handling", but with "throw exception", so the exception can not be finally processed, the exception will be thrown again. So exception handling to this does not end, it becomes the source of the throw, but also need to be "try...catch...finally" or "throws" again wrapped
- So similar to the upper level of the first step, but (the cycle began) with "try...catch...finally" or "throws" two can "catch the exception" of the thing again wrapped "throw exception" of "throws"
- And then it was
- "Throws" Wrapped in "try...catch...finally", exception can be handled, exception handling to this end
- "Throws" wrapped in the "throws", the exception can not be finally processed, the exception will be thrown again, so the exception processing to this did not end, it became the source of the throw, but also need to be "try...catch...finally" or "throws" again wrapped
- So the first step back to the upper level, exactly the same, began to circulate
Understanding the mechanism of Java exception handling mechanism