When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.
2. Exception handle, call stack
When an Exception occurred, the code will search though the call stack in a reverse order to match the handler.
3.Three kinds of Exceptios
1)checked Exceptions:a client can reasonably be expected to recover from the exception.
eg:sometimes the user supplies the name of a nonexistent file, and the constructor throws java.io.FileNotFoundException
. A well-written program will catch this exception and notify the user of the mistake, possibly prompting for a corrected file name.
2)unchecked Exceptions:a client cann't recover
error:not subject to the Catch or Specify Requirement
runTime Exceptions:These usually indicate programming bugs, such as logic errors or improper use of an API.
eg:NullPointerException, IndexOutofrangeException
4.Advantages
1)separate error-handling code from "regular" code
It makes the logical structure of the codes more clearly and easier to read.
2)propagating errors up the call back
In traditional way, you need to pass the error code by parameters.
3)grouping and differentiating error types.
Objects are natually have this character.
阅读(474) | 评论(0) | 转发(0) |