Exception handling and classification in java

Abnormal: Refers to various unexpected conditions, such as: file not found, network connection failure, illegal parameters, etc. An exception is an event that occurs during the running of the program and interferes with the normal instruction flow. Java describes a variety of different exceptions through many subclasses of the Throwable class in the API. Therefore, Java exceptions are all objects, instances of Throwable subclasses that describe error conditions that appear in a piece of code. When the condition is generated, the error will raise an exception.

1. Anomaly classification

Insert picture description here

Throwable is the superclass of all errors or exceptions in the Java language. The next layer is divided into Error and Exception

1. The Error class refers to internal errors and resource exhaustion errors of the java runtime system. The application will not throw objects of this type. If such an error occurs, in addition to telling the user, all that is left is to try to make the program terminate safely.

Error: It is an error that the program cannot handle, which means a serious problem in running the application. Most errors have nothing to do with the actions performed by the code writer, but represent problems with the JVM (Java Virtual Machine) when the code is running. For example, a Java virtual machine operation error (Virtual MachineError), when the JVM no longer has the memory resources needed to continue the operation, an OutOfMemoryError will occur. When these exceptions occur, the Java Virtual Machine (JVM) generally chooses thread termination

2. Exception has two branches, one is RuntimeException, such as NullPointerException and ClassCastException; the other is CheckedException, such as IOException and SQLException caused by I/O errors.

Runtime exceptions: all exceptions of the RuntimeException class and its subclasses, such as NullPointerException (null pointer exception), IndexOutOfBoundsException (subscript out-of-bounds exception), etc. These exceptions are not checked, and the program can choose to capture and handle it or not. These exceptions are generally caused by program logic errors, and the program should avoid such exceptions as much as possible from a logical perspective.

The feature of runtime exception is that the Java compiler will not check it. That is to say, when this type of exception may occur in the program, even if it is not caught with a try-catch statement or thrown by the throws clause, it will Compile and pass.

Non-runtime exception (compilation exception): It is an exception other than RuntimeException, which belongs to the Exception class and its subclasses. From the perspective of program syntax, it is an exception that must be handled. If it is not handled, the program cannot be compiled. Such as IOException, SQLException, etc. and user-defined Exception exceptions, generally do not customize the check exception.

2. Exception handling

1. When encountering a problem, it does not perform specific treatment, but continues to
 throw exceptions to the caller. There are three forms, one is throw, one throws, and the other is automatically thrown by the system.

Note:
 The difference between Throw and throws:

(1) Different positions: throws are used in functions, followed by exception classes, which can be followed by multiple; while throws are used in functions, followed by exception objects.
(2) Different functions: throws are used to declare exceptions, so that the caller only knows the possible problems of the function, and can give pre-processing methods; throw throws specific problem objects, and executes to throw, the function is over. Jump to the caller and throw the specific problem object to the caller. That is to say, when the throw statement exists independently, do not define other statements below because they cannot be executed.
(3) Throws represents the possibility of exceptions, and these exceptions do not necessarily occur; throw means throwing exceptions, and executing throw must throw some kind of exception object.
(4) Both are passive ways of handling exceptions (negativity here does not mean that this way is not good), only throws or may throw exceptions, but the function will not handle the exception, the real exception is handled by the function The upper layer call processing.

2. Targeted handling method: catch exception

Try{
    
    
//有可能发生异常的代码
}
 Catch(异常类 变量)
{
    
    
// 处理异常的代码,捕获
}
finally{
    
    
//一定会被执行的代码
}


Three, the difference between RuntimeException and CheckedException

1. The difference between
  RuntimeException and CheckedException RuntimeException: When defining the method, there is no need to declare that RuntimeException will be thrown, and there is no need to catch this RuntimeException when calling this method; in short, unchecked exceptions do not need try...catch...or throws mechanism to deal with.
  
  CheckedException: When defining the method, you must declare all the exceptions that may be thrown; when calling this method, you must catch its checked exception, or you must pass the exception.
  
  In short, a method must declare all checked exceptions that may be thrown; unchecked exceptions are either uncontrollable (Error) or should be avoided (RuntimeException). If the method does not declare all the checked exceptions that may occur, the compiler will give an error message.

Guess you like

Origin blog.csdn.net/weixin_49005845/article/details/110872695