Java Foundation 1: Exceptions

Source: Internet
Author: User

On the Java Foundation of the article, I think it can also be written in my other blog, is definitely original, and now share to everyone out.

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ---------


People who often write programs are certainly not unfamiliar with try...catch...finally statements. But a lot of people are not very clear about the anomaly, do not know what the meaning of this anomaly, do not know why with this exception handling, this blog for you to analyze the nature of the exception in Java working principle.

First, the definition

In the Java programming idea, you define an exception: an issue that prevents the current method or scope from continuing to execute.

This is written in the language, translated into our own language is actually the program running a problem. In particular, there is a problem with the program running, which may lead to the program running mechanism. So it is necessary to tell the developer where the problem is by throwing an exception.

Second, abnormal system

1, since the problem is to solve the problem, like life is sick to the right remedy, Java also gives us a series of exception handling classes. So how do these classes differentiate?

In general, there are two kinds of exceptions in Java, one is error and the other is exception.

Error generally can not handle also called error, exception can generally handle.

2. Exception classification

And exception can be divided into two classes, one is run-time exceptions, and one is a compile-time exception.

   A. Run-time Exceptions:

are runtimeexception classes and their subclass exceptions, such as nullpointerexception (null pointer exception), indexoutofboundsexception (subscript out-of-bounds exception), and so on, which are not checked for exceptions, You can choose to capture or not handle the program. These exceptions are usually caused by a program logic error, and the program should avoid this kind of exception as much as possible from a logical point of view.

A run-time exception is characterized by the Java compiler not checking it, that is, when such an exception can occur in a program, even if it is not captured with the Try-catch statement, it is not thrown with the throws clause declaration, and it is compiled.

B. Non-runtime exception (compilation exception):

is an exception other than RuntimeException, and the type belongs to the exception class and its subclasses. From the point of view of the program syntax is the exception that must be handled, and if not handled, the program cannot be compiled through. such as IOException, SqlException, and user-defined exception exceptions, generally do not customize check exceptions.

Note: The subclass names for exception and error in Java are suffixed with the name of the parent class.

Third, custom exception class

since exception handling in Java is called pre-built classes, can we customize the exception class? The answer is yes.

1 class Ceshiexception extends Exception 2 {  3    ceshiexception ()  4    {}  5  6     7     ceshiexception (String msg)  8    {  9        super (msg); Ten     }one}
Iv. Principles of exception handling

1. Function content If you throw an exception that needs to be detected, you must declare it on the function, otherwise you must catch it with try...catch within the function, or the compilation will fail.

2. If a function that declares an exception is called, either Try...catch or throws, the compilation fails.

3. A function if more than one exception is thrown, then the call must have a corresponding multiple catch for targeted processing. Inside there are several exceptions that need to be detected, just throw a few exceptions, throw a few, and catch a few.

V. Exception handling mechanism

In a Java application, the exception handling mechanism is: Throw an exception, catch an exception.
1. Throws an exception: When a method throws an exception, the method creates the exception object and delivers the runtime system, which contains exception information such as the type of exception and the state of the program when the exception occurred. The runtime system is responsible for finding and executing the code that handles the exception.

2. Catch exception: After the method throws an exception, the runtime system will look for the appropriate exception handler (exception handler). A potential exception handler is a collection of methods that persist in the call stack, in turn, when an exception occurs. An appropriate exception handler is the exception type that can be handled by the exceptions handler when it matches the type of exception thrown by the method. The runtime system starts with the method in which the exception occurred and then turns back to the method in the call stack until it finds the method that contains the appropriate exception handler and executes it. The runtime system terminates when the runtime system traverses the call stack without finding an appropriate exception handler. At the same time, it means the termination of the Java program.

Vi.. Last

The exception-handling framework of a program directly affects the quality of the entire project and the cost and difficulty of subsequent maintenance. With good exception handling to make our program and robust.

Java Foundation 1: Exceptions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.