Exception handling in Java language

Keep creating and accelerate growth! This is the 22nd day that I participated in the "Nuggets Daily New Plan·October Update Challenge", click to view event details

Exception handling

In the Java language, it is very clever to handle exceptions as objects, and define a base class java.lang.Throwable as the parent class of all exception classes. Among these many categories, they are generally divided into two categories: Error and Exception. - As shown in the picture:

af11d56d58833847d3078a0484f94b9.jpg

  • Note: Error is an error that the program cannot handle, such as OutOfMemoryError, etc. When these exceptions occur, the Java virtual machine generally terminates the thread. Exception is an exception that can be handled by the program itself. This exception is divided into two major categories: runtime exceptions and non-runtime exceptions.

Runtime exceptions and non-runtime exceptions

There is a very big difference between these two exceptions, also called Unchecked Exception and Checked Expception.

Runtime exception class

Runtime exceptions include: RuntimeException class, subclasses of RutimeException, etc. Such exceptions are not checked, and the program can choose to catch and handle these exceptions, or not. - Note: This kind of exception is generally caused by program logic errors, so the program should avoid this kind of exception as much as possible from a logical perspective.

non-operational exception

Exceptions other than RuntimeException belong 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, it will not be compiled. Such as SQLException, IOException and programmer-defined Exception, etc.

Java exception handling syntax

Java exception handling generally involves five keywords, namely try, catch, finally, throw, and throws. And the first three keywords cannot be used alone. - Code example: java try{ //A }catch(ExceptionType e){ //B1 }catch(ExceptionType2 e2){ //B2 }catch(ExceptionTypen en){ //Bn } finally{ //C } - Note: try statement block A represents the code that needs to be tried to run , that is, the code that requires exception monitoring. catch takes a Throwable parameter, which is used to capture and match each field type. After matching, it will not try to match other code blocks. Through the exception object, you can obtain the complete stack information in the Java virtual machine when an exception occurs in the Java virtual machine, as well as the exception information and the cause of the exception . - The finally statement block C is the statement block immediately following the catch statement. This statement will always be executed before the method returns, regardless of whether an exception occurs in the try statement block, and this statement block C will always be executed before the method returns. This is done to give the program a chance to remedy the situation . - The scope of variables in try, catch, and finally code blocks is within the code, and they cannot access each other independently. If access is needed, the variable can be placed outside the block.

throw an exception

The exceptions thrown are mainly throw and throws statements. In the method body, use throw to throw an exception. The syntax format of throw exception is as follows: java throw 异常对象名字; throws means that it is used to declare the exception category that the method may throw. It is used after the method name. The syntax is as follows: java [修饰符] 方法名 ([参数列表])throws 异常类型1,异常类型2,···,异常类型n The throws keyword is used outside the method body. The method declaration part is used to declare that certain methods may throw certain exceptions. Only if a checked exception is thrown, the method caller must handle or rethrow the exception. When the caller of the method is unable to handle the exception, it should continue to be thrown.

Common methods of throw class

  • getCause(): Returns the reason for throwing the exception. If the cause does not exist or is unknown, returns null.
  • getMessage(): Returns the abnormal message information.
  • printStackTrace(): The object’s stack trace is output to the error output stream System.err

Guess you like

Origin blog.csdn.net/y943711797/article/details/132972181
Recommended