In the development of computer language program is almost everyone's problem, so to speak: there is no exception to the program! Therefore, the computer language exception processing is a very important piece, error-correcting ability is also every programmer must have the basic ability!
Three ways to handle exception handling:
First, early judgment, avoid mistakes. Exceptions can be resolved in code, broken down into run-time exceptions and compile-time exceptions. This is often the way to handle exceptions, and is the first consideration for handling exceptions.
Second, try-catch-finally (handling Exceptions).
Syntax: variables declared within 1.try, similar to local variables, try{} statements, cannot be called. Inside a 2.catch statement is a processing statement of an exception object, which can have multiple catch statements, and the exception class object thrown in the try matches the type of the exception class in the catch, and executes the code in the catch as soon as it is satisfied. After execution, it jumps out of the multiple catch statements, which is equivalent to if-else executing a catch, and the other catch is not executed. 3.finally is optional. Go directly to the finally (if there is finally). Finally, a code that is bound to be executed, whether or not the exception is still unhandled in a try or catch, finally is a statement that is bound to be executed (the code level is only System.exit (0); can be blocked), Even if there is a retrun or break in front of the statement is still executed, then return to Retrun. 4. If the exception is handled, then the subsequent code continues to execute. If multiple exception types in a catch are "side-by-side" relationships, you can either. If more than one exception type in a catch is a "containment" relationship, the subclass must be placed above the parent class for processing. otherwise error! At the same time, Try-catch can be nested.
Third, throw exception---can not be resolved, thrown to the caller to resolve---throws and throw.
We need to throw exceptions when we cannot resolve the possible exceptions that need to be resolved by the caller itself.
The throw---throws an exception for the object, either a system-defined or a self-defined one. Meaning: Throw is followed by an execution code, and once executed, an exception is thrown. If throws is followed by a run-time exception, there is no need to require the declaration portion of the method to use throws, followed by a non-runtime exception that requires the declaration portion of the method throws plus the exception class. The exception that is thrown by a method of the parent class must have an inheritance relationship with the exceptions thrown by the child class.
Throws declares an exception---(Exception thrown for a method). Throws an exception, which can be either system-defined or self-defined. In the declaration of a class, it is written in the declaration portion of the method, followed by one or more heterogeneous names after throws. Throws is not an execution code, warning the caller that an error may occur. Subclasses cannot override the parent class's methods with more exceptions than the parent class (many refer to the exception range size);
Custom Exceptions: When we work, the project is sub-module or sub-function development, basically do not you develop an entire project, this time using the custom exception class to unify the external exception display way. Four key keys to custom exception syntax:
1. Be sure to inherit exception or throwable, otherwise it is not an exception type at all.
2. Be sure to have a exception or throwable with a parameter construction method, so that data in various objects can be loaded into the custom exception object (called using Super (ex)).
3. Must have a string with a parameter construction method, so as to achieve the encapsulation of business exceptions;
4. Add custom exceptions to their own unique processing methods (such as print logs, etc.).
Exception Handling in Java