Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the knowledge points of Java exception system?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what are the knowledge points of Java exception system". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. introduction to exceptions

Excellent program code pursues high efficiency, security, and low error rate, but the exception in the program is inevitable, reducing the frequency of exception occurrence is the key, how to deal with exception occurrence is another important aspect, the exception framework in Java system is very important for system development.

In the face of system anomalies, do not panic, although the exception is an error, but also a message sent by the system, indicating the defects of the system and areas that need to be improved.

II. API system

Many exception classes have been defined in Java's API, which are divided into two categories, error Error and exception Exception,Throwable as superclasses for all exceptions, as shown in the figure:

Error: generally, it is an underlying unrecoverable class. Generally, this kind of error is relatively serious. JVM will terminate the thread on which it is running.

VirtualMachineError: virtual machine running error

OutOfMemoryError: memory overflow

Exception: exceptions that can be caught and preprocessed by the program itself, such as catching or throwing

RuntimeException: runtime exception

CheckException: the exception has been checked and must be handled during compilation

Several classic common RunTimeException are as follows: null pointer NullPointerException; array subscript out of bounds ArrayIndexoutofBoundsException and so on.

Third, exception handling

Java exception handling keywords: try, catch, finally, throw, throws.

Exceptions should be handled in the right place, and the criteria for handling exceptions are as follows: who knows who handles it, who is responsible for handling it, and who causes who to handle it.

1. Throw an exception

That is, exceptions are not handled under the current process, one is passed directly to the caller through the method, the throws keyword is used to declare the type of exception thrown on the method declaration, and multiple types of exceptions can be declared and thrown at a time. The throw keyword is used to throw an exception object inside a method, often throwing a hint during business validation.

It is important to note that in the Spring framework, transaction triggers are mostly handled by whether or not an exception is thrown, and if the method is within the control of the transaction, the exception is caught within the method but is not thrown, then the transaction is invalid.

2. Catch exception

Usually, an exception is caught using the try-catch-finally keyword triple operation:

Try attempts to catch an exception:

If the statement execution ends in turn, catch is skipped, and if there is a finally code block, execution otherwise executes the subsequent process

If an exception is caught, the type in catch is matched. If there is no matching catch type, the exception is handed over to JVM for handling, the finally code is executed, and the code after the process is not executed.

If an exception is caught and there is a matching catch type, then jump to the catch code block for execution, the finally code will be executed, and the subsequent code execution will continue after the finally code block has been executed

Catch matches the possible exception types and compensates them, for example, if an exception occurs, an exception status needs to be updated. If there is no catch block, it must be followed by a finally block to handle resource release.

Whether Finally catches an exception or not, the finally code will be executed, which is also one of the common exception questions in the interview, such as return in the finally code block, or modifying the return value, which is mainly related to value passing and reference passing.

3. Exception log

The necessary functions of a complex business system, exception log system, are used to analyze running problems. As the core basis for continuous optimization of the system, the following pieces are usually recorded:

Exception types: analyze the key causes of exceptions

Exception information: usually simply record the content of the e.getMsg output

Exception location: quickly locate the location of the exception [class. Method]

Business parameters: a problem that can only be repeated in a specific business parameter scenario

Time node: some concurrency problems occur in a specific period of time

After the exception log is recorded, task analysis will be carried out regularly to find out where the system is prone to problems, and then continue to improve and optimize.

4. Fuse degradation

In the micro-service architecture system, a service failure or exception triggers a circuit breaker to avoid causing the whole micro-service link exception and prevent the avalanche of the whole system service. In order to alleviate the pressure of server resources, in order to ensure the normal operation of the core business.

This is the end of the content of "what are the knowledge points of Java exception system". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report