Talking about Exception Handling in Java

This blog will summarize the exception handling in Java based on the existing knowledge. The following blog is only a summary of the personal learning process. It is a great honor to be helpful to all bloggers.
This blog will briefly introduce the understanding of exceptions in Java, and how to capture exceptions in Java. I will only make a summary of myself, and I will make supplementary modifications later with in-depth study.

understand exception

The so-called exception refers to a mechanism for notifying the caller when the program encounters an error at runtime . One of the most important aspects is that if a problem occurs, it will not allow the program to go down its normal path. Another benefit is that it tends to reduce the complexity of error handling code

Array out of bounds exception Null
insert image description here
pointer exception
insert image description here
...

How to catch exceptions

Pre-knowledge:
To understand how exceptions are caught, you must first understand the concept of a guarded region , which is a piece of code that may generate exceptions, and the code that handles these exceptions is followed immediately.

Java exception handling involves five keywords, namely: try, catch, finally, throw, throws

  • try block: used to monitor the code, put the code that may cause exceptions in it, if an exception occurs, the code stops running, and an exception is thrown
try{
    
    
	//此处放置可能出现异常的代码
}
  • Catch block: used to catch exceptions, catch and process the exceptions thrown in the try statement block, which must immediately follow the try statement block, each catch clause looks like it receives one and only one special type parameter method
try{
    
    
	//此处放置可能出现异常的代码
}catch(Type1 id1){
    
    
	//处理Type1异常
}catch(Type2 id2){
    
    
	//处理Type12异常
}...
  • finally: It is mainly used to reclaim the resources in the try statement block (such as: database connection Connection, network connection socket, IO), the finally statement block will always be executed, if there is a return in the try statement block, catch statement block, and finally statement block statement, always return the return value in the finally statement block
  • throw: used in the catch statement to throw an exception and throw it to the upper level for processing
  • throws: When used in method declaration, it means that an exception will be thrown

The way exceptions are handled

Due to the wide variety of exceptions, exceptions will be handled according to different business scenarios

  • For serious abnormal problems (such as bank online payment scenarios), directly crash the program to prevent more serious consequences
  • For general abnormal problems (in most cases), the error log can be printed, and the engineer can be notified in time through the monitoring alarm
  • For recoverable exceptions (network transmission related scenarios), retry operations can be attempted

In most cases, we will implement the second method. The content of the log is the call information of the abnormal method, so that we can quickly locate the abnormal location. Method call stack: There is a
memory space in the JVM called "virtual "Machine stack" is specially used to store the call relationship between methods. When an exception occurs in the code, you can use e.printStackTrace() to check the location of the method call where the exception occurred.

exception handling process

  1. The program first executes the try statement block
  2. If an exception occurs in the code in the try block, it will jump out of the try block and check whether there is a catch statement of the same type as the exception below.
    2.1. If a match is found, the catch statement block 2.2 is executed
    . If no match is found, the The exception is passed outward, that is, passed to the upper-level caller of the method call
    2.3. If the upper-level caller does not find a match, it will continue to be passed outward
    2.4. Until the main method does not have a matching exception, it will be handed over to the JVM for processing. At this point the program will terminate abnormally
  3. Regardless of whether a matching exception type is finally found, the code in finally will be executed before the end of the method

Java Exception Hierarchy

Java has a rich built-in exception system to represent exceptions in different situations
insert image description here

  • The top-level class Throwable derives two subclasses, Error, Exception
  • Error refers to internal errors and resource consumption errors during Java runtime. The program does not throw such exceptions. This kind of exception system can't solve it. It can only inform the user and terminate the program
  • Exception is the parent class of all exception classes used to write programs
  • There is a subclass RunTimeException under Exception, and many common exception classes are derived from it

Java stipulates that exceptions derived from Error and RuntimeException are unchecked exceptions/runtime exceptions , and others are checked exceptions/compile-time exceptions

Non-checked exceptions (here refers to RuntimeException): Generally, exceptions caused by not carefully checking the code or program errors , such as null pointer exceptions, array out-of-bounds, type conversion exceptions, etc., are caused by code writing errors. These exceptions are caused during the coding process can be avoided.

Checked exception: generally not related to code programming, but related to the environment in which the program runs, such as accessing a file, but the file does not exist, which itself has little to do with the program problem

The difference between unchecked exceptions and checked exceptions:

  • Unchecked exceptions, classes belonging to the RuntimeException class and its subclasses (and derived classes), will report an error at runtime
  • Checked exceptions, within the scope of Exception, all classes except non-checked exceptions are checked exceptions, and exceptions are thrown at compile time
  • If a piece of code may throw a checked exception, it must be handled explicitly, try{…}catch{…} or throw to the upper layer

The above is a summary of the knowledge points of Java exceptions. With the deepening of subsequent learning, the content will be supplemented and modified synchronously. It will be a great honor to help all bloggers. Please correct me

Guess you like

Origin blog.csdn.net/m0_46233999/article/details/117740695