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

How to understand the exception to Java

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

Share

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

This article mainly introduces "how to understand the exception of Java". In the daily operation, I believe that many people have doubts about how to understand the exception of Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to understand the exception of Java"! Next, please follow the editor to study!

Background

Whether at work or in an interview, the abnormal part is still very important. As a Java developer, it is necessary to learn how to handle exceptions, which exceptions must be handled by yourself, which exceptions can be thrown, and so on.

There are always surprises and surprises in life, and so is our code.

I don't understand. Buddha can't bless you.

Today we're going to talk about Java anomalies. We can think of Throwable as the root class (ancestor) of Java exceptions. Because there are too many implementation classes, a few common implementation classes are roughly listed below.

Error

Errors: system-level errors and exceptions that programs do not have to handle are internal errors or hardware problems in the Java runtime environment.

For example, insufficient memory resources and so on.

For this error, the program is basically powerless and has no choice but to quit running, which is thrown by the Java virtual machine.

Exception

Exception: an exception that needs to be caught or handled by a program. it deals with problems caused by flaws in programming or general problems caused by external input, which the program must deal with.

Exception is divided into run-time exceptions and checked exceptions.

RuntimeException (runtime exception)

Represents exceptions that cannot be recovered by the program, usually because the wrong operation has been performed and it is recommended to terminate the logic, so the compiler does not check for these exceptions.

CheckedException (checked exception)

Is an exception that the program can handle, that is, the program can fix it (the program accepts the exception and handles it), so it is called the checked exception.

There are two ways to handle Checked exceptions:

The current method clearly knows how to handle the exception, and the program should use the try--catch code block to catch the exception and then fix the exception in the corresponding catch module.

The current method does not know how to handle this exception, and it is declared to run out when the method should be defined.

Common methods of Throwable class?

The getMessage () method returns the details of the occurrence of the exception.

The getCause () method: gets the Throwable exception that causes the current Throwable exception.

The getStackTrace () method: gets the exception information encapsulated by the Throwable object.

The printStackTrace () method: print on the console.

Please list 5 common runtime exceptions?

NullPointerException

IndexOutOfBoundsException

ClassCastException

ArrayStoreException

BufferOverflowException

What are the keywords for exception handling?

Throw exception: throw,throws

Catch exception: try,catch,finally

The difference between throw and throws?

Throw, used to explicitly throw an exception instance in the program.

Throws, used to indicate exceptions that are not handled in this method. Each method must explicitly indicate which exceptions are not handled so that the caller of the method can prevent possible exceptions. Finally, multiple exceptions are separated by commas.

What is the importance of finally statement blocks in exception handling?

The block of finally statements is executed regardless of whether the program has an exception or not, even when there is no catch declaration but an exception is thrown.

Finally statement blocks are typically used to release resources, such as Imax O buffers, database connections, releasing locks, and so on.

Abnormal use mode

Exception handling rules

How do I customize exceptions?

In addition to the exception classes defined by JDK, we can also customize exception classes according to the exception situation of the business during the development process.

User-defined exceptions should inherit the Exception base class, and if users want to customize Runtime exceptions, they should inherit the RuntimeException base class.

When defining an exception, there are usually two construction methods:

Non-parameter construction method

There is a parameter constructor, which is a string, which is used as a description of the exception information, that is, the return value of the getMessage () method (for example, "user password is incorrect", "xx interface return parameter is empty", etc.).

However, it may not be so simple in the project, and it may be encapsulated again.

But basically there are two parameters: code+msg

Code: mainly classifies the exceptions in the business and defines them in combination with the code of Http.

Msg: mainly information prompts for business exceptions.

How are the custom exception classes in your project encapsulated? If you don't know, go back and look through how the customizations in your project are written, which combines learning with practice.

At this point, the study of "how to understand the exception to Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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