Exception handling mechanism for Java

Source: Internet
Author: User

A throwable interface is provided in Java, where errors and exceptions are implemented for this interface.

In the case of errors, programmers often do nothing, and once that happens, only a way to gracefully exit the program. For example, common errors include the following:

Virtualmachineerror:java the virtual machine is running incorrectly, or the virtual machine does not have sufficient resources to run

OutOfMemoryError: A Java Virtual machine cannot locate an object because it is out of memory, or is not out of memory to be thrown when garbage collection is not possible

Stackoverflowerror: Thrown when stack overflows

Linkageerror: There is a dependency between two classes, when the dependent class changes is thrown

Due to the error of the program ape powerless, we can only do is to pay attention to the exception, the specific exception to deal with.

Exceptions are divided into run-time exceptions and other exceptions. Runtime exceptions are unchecked by the programmer's improper handling of the code, while other exceptions are caused by external causes and are checked.

Here's a look at the difference between checked and unchecked anomalies:

The checked exception must be caught and the error handler handler specified, otherwise the compilation cannot pass.

Unchecked exceptions can be handled without problems at compile time, but appearing at execution time will cause the program to fail.

The appearance of unchecked anomalies is actually due to our improper handling of the code, so we can eliminate these exceptions by changing the code. And for the checked anomaly, which is caused by external reasons, most of the user is improper operation, for this problem we can not be a good way, so should be for the checked exception extra attention. The keywords for checked exceptions are mainly

Try

Catch

Finally

Throws

Throw

Its use is similar to the following:

Place the code snippet that might throw the exception in the try and catch the exception with the following catch, since we may have requested a lot of resources in the program, and once the program throws an exception, the statements after the exception statement thrown in the try will not be executed, but instead jump directly to the catch statement. This can cause a lot of resources not to be properly closed, this time finally has played a role, regardless of whether there is an exception occurred, finally will be executed, so that those who closed the resources of the statement in the finally is executed.

For throw, his role is to throw out exceptions, such as

Exception e = new Exception;

Throw e;

And throws is in a class or method, declaring what exceptions my class or method might throw, and handing these exceptions to the previous layer. (Note that an exception, if caught by a catch, does not have to be declared in throws, only those exceptions that are not handled in this class, expect the upper function to handle the exception should be thrown and declared with throws.) )

Exception handling mechanism for Java

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.