I. ...
Exception handling is a fault-tolerant processing mechanism. By constructing a trap to capture the runtime's predictable error, the error is properly handled so that the program can continue running without crashing.
Ii. who would ...
Exceptions are raised by the system environment, which is captured by the developer and left to the developer or user for processing.
Third, when do ...
Programming is not intended to handle the error immediately;
Iv. Where do ...
Anticipate impending errors, for example: preparing to write files to SDcard, but possibly SDcard not installed, which will result in file access errors.
V. How to ...
1. Defining exceptions
The exception class should be defined in its own class code file.
Inherit from standard exception (Exception),
Public class extends Exception { // no parameter constructor public customexception () { } // constructor with parameter Public customexception (String message) { // Call the construction method of the base class Super( message);} }
You can also implement Throwable to construct a custom exception.
2. Throw an exception
Exception thrown in method signature (throws):
void MyMethod () throws customexception;
Throw exception (throw) in method body:
Throw customexception;
3, catch the exception, and do the appropriate treatment
Try-catch-finally
Catch exception in try code block
Handling exceptions in Catch code blocks
Finally code block to do the cleanup work
Java Custom Exceptions (Exception, throws, Try-catch)