Checked exception, unchecked exception, exception handling, actively throwing exception

1. Exception definition

An exception is an error operation that hinders the normal execution of the program during program execution. As long as an exception occurs during the execution of a Java statement, an exception object will be created. Throwable is the parent class of all exceptions. It has two direct subclasses Error and Exception. Among them, Exception is further divided into checked exceptions and runtime exceptions (runtime exceptions). ; Error represents a system error, which usually cannot be expected and recovered (such as JVM crashes, insufficient memory, etc.); checked exceptions (Checked exceptions) can be expected in the program and need to be repaired (such as we must catch FileNotFoundException exceptions and provide useful Information and appropriate logs for debugging, Exception is the parent class of all checked exceptions); Runtime Exceptions are also called unchecked exceptions. For example, we must confirm the length of the array before retrieving array elements, otherwise it may Will throw ArrayIndexOutOfBoundException runtime exception, RuntimeException is the parent class of all runtime exceptions

Unchecked exceptions (RuntimeException) : This type of exception is a logic problem for programmers and does not need to be caught or thrown. The program automatically enters exception information. Common exceptions include: NullPointerException, ClassCastException, ArrayIndexsOutOfBoundsException, and
checked exceptions (non-RuntimeException) : This type of exception is caused by some external accidental factors, and this type of exception must be caught or thrown ; The checked exception can control the obligation logic, and can handle the problems brought by the exception in the catch. Common exceptions are: Exception, FileNotFoundException, IOException, SQLException.
The difference between a checked exception and an unchecked exception is : whether the caller must be forced to handle the exception, if the caller is forced to handle it, then the checked exception is used, otherwise the unchecked exception (RuntimeException) is selected

 

2.Error与Exception

Error represents a system-level error, which is an internal error in the Java runtime environment or a hardware problem. The program cannot be expected to handle such problems. There is no choice but to exit the operation. It is thrown by the Java virtual machine. Exception represents the exception that the program needs to catch and handle. It is a problem that arises from imperfect program design and can be handled by the program.

 

3. Exception handling

try block: used to catch exceptions. It can be followed by zero or more catch blocks. If there is no catch block, it must be followed by a finally block.
Catch  block: Used to handle the exception caught by try.
Finally block: Regardless of whether the exception is caught or handled, the statements in the finally block will be executed. When a return statement is encountered in a try block or catch block, the finally block will be executed before the method returns.

Note: When there is a return statement in both the try statement and the finally statement, the content of the finally statement will be executed before the method returns, and the return value of the finally statement will overwrite the original return value. as follows:

public class Test {
public static int f(int value) {
try {
return value * value;
} finally {
if(value==2){
return 0;
}
}
If f(2) is called, the return value will be 0, because The return value of the finally statement overrides the return value of the try block.

4. Throw an exception

The throws keyword is usually used when declaring methods to specify the exceptions that may be thrown. Multiple exceptions can be separated by commas. When the method is called in the main function, if an exception occurs, it will

Throw the exception to the specified exception object

​publiC class Shoot {

static void Pop() throws NegativeArraySizeException {

//Define the method and throw NegativeArraySizeException

int [] arr = new int[-3];//Create an array so that its length is -3 so that it can be abnormal

}

pub1ic static vo1d main (String [] args) {

try{ .

Pop() ;

} catch (NegativeArraySizeException exp) {

System. out .println("Pop() method throws an exception, the exception information is: "+exp);

}

}

}

Exception handling method

Three exception handling methods are defined in the Throwable class.
String getMessage()_ returns a brief description of this throwable.
String tostring() Returns the detailed message string of this throwable.
void printstackTrace() JVM prints the exception object, this method is the default, the exception information printed is the most comprehensive

Multiple exception handling methods

1. Multiple exceptions are handled separately.
2. Multiple exceptions are caught once and processed multiple times.

     Note for multiple catches in one try:
     The exception variables defined in the catch, if there is a child-parent relationship. Then the exception variable of the subclass must be written above, otherwise an error will be reported

. 3. Multiple exceptions will be caught and processed once.

try {
int[] arr = {1,2,3};
//System. out . println(arr[3]);//ArrayIndexOutOfBoundsException: 3
List<Integer> list = List.of(1, 2, 3);
System. out . println(list. get(3);//IndexoutOfBoundsException: Index 3 out-of-bounds for length 3
}catch (Exception e){
System. out. println(e);
}


Runtime exceptions may not be handled if they are thrown. Neither capture nor declare to throw.
If finally has a return statement, always return the result in finally to avoid this situation.
If the parent class throws multiple exceptions, when the subclass overrides the parent class method, it throws the same exception as the parent class or the parent class exception Subclass or not throw an exception.
The parent class method does not throw an exception, and the child class cannot throw an exception when it overrides the parent class. At this time, the subclass generates the exception. It can only be caught and processed, and cannot be declared to be thrown.
After the try/catch, a finally code block can be added, and the code in it will be executed, usually for resource recovery.


 

Guess you like

Origin blog.csdn.net/weixin_51980491/article/details/112739191