Java Exception handling mechanism

Source: Internet
Author: User

Java Exception handling mechanism introduction

Exceptions are exceptions, which are errors that occur during a program's run, and handling these exceptions becomes exception handling. Some languages judge whether a program has an exception by its return value, and then handle it accordingly according to the different return values. There are several disadvantages to doing so:

1. First, the main program and the process of handling exceptions mixed together, resulting in a program too complex, the readability of the program is poor

2. Different programmers understand the return value is not uniform, the program maintainability is not enough

3. There is no mechanism to ensure that exceptions to the program must be handled, which can cause system memory leaks

For these questions, Java provides a good idea for the solution:

Java provides a uniform exception handling mechanism: When a method produces an exception, the Java virtual machine produces an exception object that carries the state information at the time the exception occurred, the method call can catch the exception, or it can evade the exception, and the exception is passed up the method call stack (if none of the methods catch the exception, Exceptions are handled by the Java Virtual Machine), and after the exception is processed by Java, the program executes the next sentence that the exception occurred.

Java Exception Framework

Java exceptions can be divided into two categories: error and Exception:error are serious errors, not recovery errors, which do not need to be processed in the program. RuntimeException is an error that occurs during a program's operation and does not require capturing

The general exception is an exception that requires exception handling

Java Exception handling mechanism

Java has two kinds of exception handling mechanisms:

1. Use try{}catch{}finally{} for processing and handling yourself

2. Processing with throws and throw, the method's caller handles

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

1. Use a try to wrap the code that may have an exception, when an exception occurs, the Java Virtual machine generates an exception object, the program moves to the first catch block, matches the exception until the last catch is matched, or if there is no match, throws the exception to the program's coupon.

Exceptions are matched to three conditions:

1. The exception type of the caught exception and catch is the same

2. Subclasses of exceptions caught when catching exceptions

3. The caught exception is an implementation of the exception to the catch's interface

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

throws in the declaration of the program, representing the exceptions that this program might throw

The throw represents an exception to the code that will be thrown if it occurs.

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

The principle of exception handling:

1. Handle the exception, do not ignore it

2. Catch only specific exceptions, do not catch all the exceptions at once

3. Good at using finally blocks

4. Try statement not too large

Java Exception handling mechanism

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.