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 is the analysis and handling method of Java exception type

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I will talk to you about the analysis and handling methods of Java exception types, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Foreword:

Java exception is familiar to everyone. But it may not be so clear about how to classify it, how JVM handles it, how it is handled in the code, how it should be used, how it is implemented at the bottom, and so on. Based on this, the following is a detailed overview of the exception types, implementation and use of how to pay attention.

I. implementation and classification of exceptions

First, take a look at the structure diagram of the exception class.

The above figure can briefly show the exception class implementation structure diagram, of course, the above figure is not all exceptions, users can also customize the exception implementation. The above figure is enough to help us explain and understand the exception implementation:

1. All exceptions are inherited from Throwable and are the common ancestor of all exceptions.

2.Throwable has two subclasses, Error and Exception. Where Error is an error, and all compile-time errors and system errors are thrown through Error. These errors indicate that the failure occurred in the virtual machine itself, or when the virtual machine attempted to execute the application, such as Java virtual machine running error (Virtual MachineError), class definition error (NoClassDefFoundError), and so on. These errors are untraceable because they are outside the control and processing power of the application, and the vast majority are conditions that are not allowed to occur while the program is running. For a well-designed application, even if an error does occur, you should not essentially try to deal with the exception caused by it. In Java, errors are described by a subclass of Error.

3.Exception is another very important exception subclass. The exceptions it specifies are those that the program itself can handle. The difference between exceptions and errors is that exceptions can be handled, while errors cannot be handled.

4.Checked Exception

Detectable exceptions, which are very common when coding, and all checked exception need to be handled in the code. Their occurrence is predictable, a normal situation, and can be dealt with reasonably. Such as IOException, or some custom exceptions. Except for RuntimeException and its subclasses, it is checked exception.

5.Unchecked Exception

RuntimeException and its subclasses are unchecked exception. For example, NPE null pointer exception, arithmetic exception ArithmeticException with divisor of 0, etc., this kind of exception occurs at run time and cannot be caught and processed in advance. Error is also a unchecked exception and cannot be pre-processed.

2. Handling of exceptions

Exception handling in the code is actually the handling of detectable exceptions.

1. Through the try...catch statement block to handle:

Try {/ / Program Code} catch (ExceptionName E1) {/ / Catch Block}

The Catch statement contains the declaration that you want to catch the exception type. When an exception occurs in the protection code block, the catch block after the try is checked.

If the exception that occurs is contained in a catch block, the exception is passed to that catch block, just like passing a parameter to the method.

two。 In addition, it can not be handled in a specific location, thrown directly, and then processed to the upper layer through throws/throw. Specifically, if a method does not catch a checking exception, then the method must be declared using the throws keyword. The throws keyword is placed at the end of the method signature. You can also use the throw keyword to throw an exception, whether it is newly instantiated or just caught.

The declaration of the following method throws a RemoteException exception:

Import java.io.*;public class className {public void deposit (double amount) throws RemoteException {/ / Method implementation throw new RemoteException ();} / / Remainder of class definition}

3. Finally keyword

The finally keyword is used to create blocks of code that execute after the block of try code.

The code in the finally code block is always executed regardless of whether an exception occurs.

In the finally code block, you can run wrap-up statements such as cleanup types.

The finally code block appears at the end of the catch code block with the following syntax:

Try {/ / program code} catch (exception type 1 exception variable name 1) {/ / program code} catch (exception type 2 exception variable name 2) {/ / program code} finally {/ / program code} after reading the above, do you have any further understanding of Java exception type analysis and handling methods? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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

Development

Wechat

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

12
Report