Exception handling
Three kinds of exceptions:
- Check for exceptions: Checking for exceptions is usually a user error or a problem that cannot be foreseen by the programmer
- Run-time exception: a run-time exception is a type of exception that can be avoided by the programmer when a program is running
- Error: The error is not an exception. is an issue that cannot be controlled by users and programmers. Errors are usually ignored in the code, and although you want to fix the problem in a program, you rarely make a difference to a single error. An error is similar to an exception when it occurs in a program. Exceptions and errors can cause our application to crash
Abnormal control Flow
An exception is an object that is thrown by a method. This method is pressed into the memory's method call stack when a method is invoked. When a method throws an exception, the method is ejected from the call stack, and the resulting exception object is thrown to the previous method in the stack. There are three options for handling exceptions:
- Catch the exception and not let him go down the stack and throw it down
- Catch the exception and continue to throw down
- Without catching exceptions, the exception object continues to be thrown to the main () method below the call stack
Regardless of how many methods are in the call stack, the program control process will continue to execute down the call stack. There is no method in the call stack that either captures the exception or catches it, throws it again, or simply does nothing to get him into the next method.
Throwable class
Only objects of the Throwable class can be thrown by the JVM. He has two subcategories of exception and error. Error is the parent class of all Java error classes and is severely unrecoverable. Exception is the parent class for all exceptions, including run-time exceptions and check exceptions
Catching exceptions
In Java, you typically use the try and catch keywords to catch exceptions. Use the Try/catch keyword to wrap the code that might produce an exception, where the code is called the protected code.try{//被保护的代码}catch(异常的名称 e){//捕获块}
The Catch statement contains the type declaration of the exception that we want to catch. If an exception occurs in a try block, the catch block checks the exception, and if the exception type and the Catch statement are listed, the exception object is passed into the catch block. It is easy to say that when an exception occurs in a try, it jumps directly into the catch, and if it executes normally, the catch will not execute.
There is also a syntax try{}catch(){}finally{} that finally executes, no matter what happens before, and is typically used to close resources
Multiple Catch blocks
When the protected code produces several different types of exceptions, the try follows multiple catch blocks and jumps to the corresponding catch block when an exception is generated.
In a catch block, the latter exception must be of the same rank as the previous exception or the rank range is greater than the previous exception
Exception declaration and exception handling
In exception handling, we should avoid placing statements in protected code unless necessary. However, when we do the test, we often put the entire program code in a large Try/catch block that catches the exception, which avoids some compilation errors that do not conform to exception handling or life rules.
Exception declaration
If a method does not handle an exception, the method must use the throws keyword to declare the exception. The keyword must be separated by commas at the end of the method where public void test()throws Exception a method can throw multiple exceptions
Throw exception
Keyword: throw
We can throw an exception array subscript out throw new ArrayIndexOutOfBoundsException(5) -of-bounds exception with throw, invalid index 5. You can also instantiate an exception before throwing itArrayIndexOutOfBoundsException e=new ArrayIndexOutOfBoundsException(5); throw e;
PS: Subclasses cannot throw more exceptions than parent classes
User-defined exceptions
- All exceptions must be subclasses of Throwable
- If we want to write a detection exception that can be automatically enforced by exception handling or claim rules, we need to inherit the exception class
- If you want to write a run-time exception, you must inherit the RuntimeException class
Java Learning (10)