Java Exception handling mechanism

Source: Internet
Author: User
Tags finally block throwable

An unexpected event may occur during the run of a Java program, which prevents the program from executing as expected by the programmer, which is an exception. When an exception occurs, we should not let the program go to its own, return the output error message to the user, so we need to handle the exception, Java has a very good exception handling mechanism. Directly on the Java exception class between the structure diagram, at a glance to understand a lot of problems.

1. The Throwable class is the top-level parent of the Java exception type, and an object can only be an instance of the Throwable class (or subclass), and it can be an exception object to be recognized by the exception handling mechanism.

2. The Throwable class derives the error class and the exception class.

Error: The error class and an instance of its subclass represent the JVM itself. Errors cannot be handled by programmers through code, and error rarely occurs. Therefore, we should focus on the exception class under the branch of the parent class exception.

Exception : The exception class and its subclasses, which represent the various undesirable events that are sent when the program is run, can be used by the Java exception handling mechanism and are the core of exception handling.

3. Non-check exceptions (unchecked exception): Error and RuntimeException and their subclasses. Javac does not prompt and discover such exceptions at compile time, and does not require the program to handle these exceptions.

Check for exceptions (checked exception): Other exceptions except error and runtimeexception. Javac forces programmers to prepare for these exceptions (Try...catch. Finally.. Or the method is thrown with throws clause life, otherwise the compilation will not pass. Such exceptions are usually caused by the running environment of the program, such as FileNotFoundException, that is, the program changes the operating environment may not find the corresponding files.

Custom exceptions

If you want to define an exception class, you need to extend the exception class so that the custom exception class belongs to the check exception , and if you want to customize the non-check exception class , you need to extend the RuntimeException .

By convention, the custom exception class should contain the following constructors:

(1) A non-parametric constructor;

(2) A constructor with a string parameter and passed to the constructor of the parent class;

(3) A string parameter and a throwable parameter are passed to the parent class constructor;

(4) A constructor with the Throwable parameter and passed to the constructor of the parent class.

1  Public classIOExceptionextendsException2 {3     Static Final LongSerialversionuid = 7818375828146090155L;4  5      PublicIOException ()6     {7         Super();8     }9  Ten      PublicIOException (String message) One     { A         Super(message); -     } -   the      PublicIOException (String message, throwable cause) -     { -         Super(message, cause); -     } +   -      PublicIOException (throwable cause) +     { A         Super(cause); at     } -}

Exception handling to avoid problems

1, do not use return in the finally;

2, do not throw an exception in the finally;

3, reduce the task of finally, do not do some other things in Finally, finally block is only used to release resources most appropriate;

4, will try to write all the return on the last side of the function, not try...catch. Finally in.

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.