16. Exceptions
16.1 a phenomenon that affects the program's normal operation during program execution.
16.2 exception syntax
try{
code block
}catch (Exception type E) {
}catch (Exception type 2 e2) {
}... {
}finally{
}
Note: Try: A block of code that represents an exception that might occur
Catch: Grab exceptions and handle them
Can crawl multiple exceptions, the range of exceptions to small to large crawl
And only the first matching exception type is executed
Finally: Eventually, the code block in finally executes regardless of whether an exception occurs.
In addition to the virtual machine Stop (system.exit (1)) in this case
Note: The execution order of finally and return: Return is executed first, the returned result is saved in the returned result area, and no return
Then execute the FINALLY,Z and return the results saved in the result area to the caller.
16.3 Throuws throws an exception
A. That is, when the current method does not resolve the exception, the exception must be given to the previous caller to handle
B. Syntax
Access modifier return value type method name (argument list) "throws exception" {
}
17.java Anomaly System
Throwable
--error: (Errors that cannot be handled by the program)
--exception: Errors that can be crawled or thrown with the program
--Check for exceptions (non-runtime Exceptions): Exceptions that occur when compiling truncation
SqlException,
IOException,
ClassNotFoundException
--Non-check exception (run-time exception runtimeexception): The exception that occurs during the run phase
NullPointerException,
ArrayIndexOutOfBoundsException,
ClassCastException
Note: The checked exception is the one that needs to be handled
Run-time exception, can not be handled
Java exception and exception system