purpose
l When an error occurs or is executed, the normal program is stopped immediately, find the appropriate Exception Handler to handle the unexpected conditions thrown by the Throw Statement.
classification
l can be used in Throw Statement: Error, Throwable, Exception, RuntimeException
l All Exception is a Subclasses of java.lang.Throwable, can be divided into three categories:
WITH SUBCLASSES
Need catch
cause
Java.lang.Error
NO
Severe conditions such as Out of Memory almost impossible to clean up. In the program, you can Catch this class, but can't do any remedies after catch, few people will go to the Exception of Catch.
Java.lang.Runtimeexeption
NO
Program bug, such as int i = 3/0; java.lang.arithmeticeXception will occur.
Java.lang.exeption (except java.lang.RuntimeExeption)
YES
Environmental issues, such as the program to read a web page but the website is a machine, etc. Unable to prevent such status, only Catch this Exception.
Handle
l Requires CATCH Exception method:
Handling
Use try-catch-finally statement to handle it.
DECLANG
Using the Throws clause, the declaration you might throw out some Exception and throw Excetions to Caller processing. Caller does not need to use try-catch-finally block to handle it.
l Exception time processing method:
Find appropriate catch block
Execute Catch Block, then perform Finally Block, and then execute it to restore normal. (Ie Statement after executing Finally Block)
No Catch Block found
Execute the Finally Block, then give the control to the outer TRY / CATCH / FINALLY STATEMENT. If all outer layers don't catch this Exception, the program is extremely terminated.
Execution order
l try / catch / finally is not reversed. After TRY, you can only follow the Catch or only with finally, but there is no Catch without Finally.
l Catch Block sequence rules:
n Particular Exception Block must define before, General Exception must be defined behind. (when catchment, before, throwable is behind)
n If the previous Catch Block Exception Object, the back Catch Block can no longer declare Catch that Class or the Subclass of the Class.
note
l Defining the program in Finally Block will be executed, even if Exception occurs but does not write Return in TRY or CATCH block, or will be executed unless it is executed in TRY or CATCH block. System.exit () or Exception occurs in Finally Block.
l Finally Block action covers the actions of TRY or CATCH Block. That is, Finally Block Return or Throw Statement will cover the Return or Throw Statement of Try or Catch Block. l The exceptions throwing out of Overriding Method cannot exceed the Exceptions scope throwing out of Overridden Method, otherwise compiler error will occur. (Ie, EXCEPTION, which can be the same as the parent category Exception or for the Exception's Subclass, but not larger than the parent class EXCEPTION)