11. Exception Handling
ProgramProblem:
1. Error: syntax error and runtime error;
Running errors refer to running environment crashes or hardware problems caused by program errors. This error cannot be used.CodeSolve the problem.
2. Exception: compile-time exception/check exception (checked) and runtime exception/unchecket ).
The exception can be solved using code.
Exception search: Search for the first line of code written by yourself from the top down.
Handling exception:
1. Prior judgment: Before an exception occurs;
2. Exception Handling Mechanism (post-capturing): After an exception occurs (try-catch-finally ))
Throwable class:
Thorwable is the parent class of all program problems. It has two sub-classes: Error and exception.
Error is the parent class of all errors;
Exception is the parent class of all exceptions.
The exception includes runtimeexception (the parent class of all runtime exceptions) and non-runtimeexception compiling exceptions.
Because all exceptions inherit from the throwable class, we can also call the methods of the throwable class on any caught exceptions.
1. Public String getmessage (): returns detailed information about an exception that has occurred. This method is initialized in the constructor of the throwable class.
2. Public thowable getcause (): returns the cause of the exception described by the thowable object. This is because the throwable constructor or initcause () method is used for initialization.
3. Public String tostring (): returns a brief description of the thorwable class.
catch exception:
write try and catch:
try cannot exist independently and must be followed by catch or finally.
try {
code where exceptions may occur is written here
} catch (exception variable declaration is used to determine whether exceptions occur in this catch) {
}< br> If a try exception occurs, the following statements are not executed. Skip to the Catch Block to handle the exception. If no exception exists, skip the Catch Block.
multiple catch blocks:
multiple catch blocks are matched in order, and the referenced Catch Block matches the Catch Block.
When catch is followed by multiple catch, the exceptions caught in catch are inherited, and the Child class is written after the parent class.
finally Keyword:
finally is the code that is always executed regardless of the situation. It is executed before return and break. It is usually the resource shutdown and memory cleanup actions. Input. Next (); takes the buffer data and processes the statement.
the code level is only system. Exit (0). disabling a virtual machine can block finally actions.
Exception Handling and declaration rules:
Instanceof can also be used to identify exceptions.
Throws Keyword:
Purpose: Warn method callers that an exception may occur in this method. It is written in front of the curly braces behind the method declaration. It can also be used in the main method. The exception types can be multiple, separated by commas.
Throw an exception:
The throw keyword is used to throw an exception. This exception can be a new exception instance or an exception we just caught. Throw statement will cause the current code to stop immediately and the exception will be thrown to the method call
Use the previous method of stack, that is, the method caller.
Throww new arrayindexoutofboundsexception (5 );
An exception occurs when the method encounters throw, regardless of the try block. If it is not a runtimeexception, it must be written in throws.
The keyword throws is used to declare an exception. It is only used in the method signature. The keyword throw is used to throw an exception. It can be used wherever we want to throw an exception.
Method rewriting and exceptions:
The sub-method cannot throw more exceptions than the parent method. (More include quantity and range)
Exception throw mechanism:
1. the Java Virtual Machine will generate the exception object;
2. Submit a method for reporting exceptions;
3. If this method is not processed;
4. It is thrown to the method caller;
5. If no processing is available, the request is sent to the caller;
6. Finally, it is thrown to the virtual machine and the program is terminated.
Custom exception:
1. The exception class must be inherited;
2. there must be three structures with parameters (3): 1, prediction2, throwable3, and string (for business exceptions );
3. Throw an exception in a method;
4. Capture and handle exceptions in another method.