Java Exception Handling

Source: Internet
Author: User
Tags throw exception

In a Java application, the exception handling mechanism is: Throw an exception, catch an exception.

Throw exception : When a method throws an exception with an error, 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.

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.

Because of the non-availability of runtime exceptions, in order to make the application more reasonable and easier to implement, Java rules that runtime exceptions are automatically thrown by the Java runtime system, allowing applications to ignore runtime exceptions.

For all the exceptions that can be checked, the Java rule is that a method must catch, or declare the throw method. That is, when a method chooses not to catch an exception, it must declare that it will throw an exception.

The ability to catch exceptions requires a matching type of exception handler. The exception that is caught may be an exception that is thrown by an individual statement, or an exception that is thrown by a method called or by the Java Runtime System. In other words, the exception that a method can catch must be the exception that Java code throws on the premises. Simply put, the exception is always first thrown and then captured.

The try-catch-finally statement is in the general syntax form:
    1. try {
    2. //program code that may occur exception
    3. } catch (Type1 id1) {
    4. //Capture and process the exception type thrown by the try Type1
    5. } catch (Type2 id2) {
    6. //Capture and process the exception type thrown by the try Type2
    7. } finally {
    8. //The statement block that will be executed regardless of whether an exception occurred
    9. }

Java Exception Handling

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.