Exceptions in Java

Source: Internet
Author: User
Tags finally block

When the program is running, there is a situation outside of expectations, which prevents the program from executing as expected by the programmer, which is an exception.

For exceptions, Java provides an excellent workaround: exception handling mechanisms. The usual processing mechanism allows the program to handle exceptions in the event of an exception, in accordance with the code's pre-set exception handling logic, to allow the program to return to normal as much as possible and continue execution, and to keep the code clear.

Exceptions in Java can be raised when a statement executes in a function, or it can be manually thrown by the programmer through a throw statement. Whenever an exception is generated in a Java program, the JVM creates an object, saves the exception's specific information to the object, and throws the object through the Throw keyword. If the function writes the code that handles the exception, it executes the code, and if not, returns the exception to the caller of the function and ends the function. If the caller of the function writes the code that handles the exception, executes the code, and if not, saves the location of the exception to the object, and then continues to throw the object up one level. If there is no code to handle the exception, the object will eventually be thrown to the JVM. The JVM will output the exception information stored in the object to the console and then end the program.

System and classification of anomalies:

As you can see, the Throwable class is the top-level parent class for all errors and exceptions.

Errors: The error class and its subclasses, which are usually the JVM's own errors (System errors), are not handled by the programmer through the code. Therefore, we generally care about the exception class and its subclasses that can be processed by code.

Exceptions: The exception class and its subclasses are generally undesirable things that occur when a program is run. Can be handled by the Java exception handling mechanism.

The exception class is also divided into running exception classes (non-check exception classes) and non-running exception classes (which can be checked for exception classes).

The Runtimeexcetion class and its subclasses are run exception classes (non-check exceptions). It is a variety of exceptions thrown by the interpreter while the program is running, and can occur anywhere and at very high frequency. Therefore, in order to avoid the huge overhead of system resources, the compiler does not check for such exceptions. For this type of exception we can write code processing, or we can not handle it. This kind of exception appears, generally our code has the problem of writing, can be avoided by correcting the code. such as arithmetic exception (except 0) arithmeticexecption, type cast error classcastexception, null pointer exception nullpointerexception, corner label out of bounds exception ArrayIndexOutOfBoundsException and so on.

In addition to the RuntimeException class and its subclasses, other subclasses of the exception class are non-running exceptions (check for exceptions). It will be checked by the compiler at compile time. Such exceptions must be handled abnormally, and if not processed, the compilation does not pass. This kind of exception is because the program may be running in a variety of unknown environment, and the programmer can not interfere with the user how to use the program he wrote, so the programmer should be prepared for such an unusual moment. This kind of anomaly has sqlexception, ioexception,classnotfoundexception and so on.

How to handle Exceptions:

There are two ways in which Java code processing can check for exceptions.

1, through the throws keyword declaration of possible exceptions, to the function of the caller to deal with.

2. Catch and handle the exception by try{} catch{} finally {} statement.

It is important to note that:

1. When declaring an exception,

If more than one exception is thrown in a function, each exception is handled;

It is also possible to declare a common parent class;

2. When catching anomalies,

If there are multiple exceptions, it is necessary to capture them several times, handle them separately, and use multiple catch;

If you are working with code, you can use a catch capture;

If more than one catch exception type under the same try block has a parent-child relationship, you should put the subclass exception in front, and the parent exception behind, so that each catch block has a sense of existence.

Finally block regardless of whether the exception occurs, it must also execute as long as the corresponding try executes. There is only one way to keep the finally block from executing: system.exit (). So the finally block is usually used to do the resource release operation: Close the file, close the database connection, and so on.

In the same try...catch...finally block, if an exception is thrown in the try and there is a matching catch block, then the catch block is executed before the finally block is executed. If there is no catch block match, the finally is executed first, and then the outside caller is searched for the appropriate catch block.

In the same try...catch...finally block, the try exception occurs, and the exception is thrown when the matching catch block is handled, and then finally is executed: the finally block is executed first, and then the outer caller is searched for the appropriate catch block.

Exceptions for method overrides in inheritance:

A subclass-overridden function cannot declare more compilation exceptions than a function in the parent class and can only declare the exception or its child exceptions;

The override function declares a run-time exception and has no effect on the method override.

Custom Exceptions:

There are already some exception classes in Java that describe the problems that often occur in a program. In addition, we can also customize exceptions.

Steps:

1, custom an exception class, Inherit Exception or runtimeexception;

2, provide a constructor for this class, in the constructor with Super Call the parent class has a parameter function.

Exceptions in Java

Related Article

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.