Summary of Java Exceptions

Source: Internet
Author: User
Tags terminates

1. Classification of exceptions

1) Checked exception: This type of exception is a subclass of exception. The exception is handled by an up-throw mechanism, and if a subclass can produce a throws, a exception must also be in the parent class. Possible problems: Low code efficiency, high coupling. This exception mechanism is not used in C #.
2) Unchecked exception: Such exceptions are runtimeexception subclasses, although RuntimeException is also a subclass of exception, but they are special, they cannot be passed through the client Code to try to solve, so called unchecked exception. RuntimeException (Runtime exception), no Try...catch required ... or throws mechanism to handle the exception.

1.2Java exception

Java provides two main types of exceptions: RuntimeException and Checkedexception; general exceptions (Checkedexception) mainly refer to IO exceptions. For this exception, the JVM requires that we have to cathc it, so, in the face of this exception, write a whole bunch of catch blocks to handle the possible exceptions. Run-time Exception (RuntimeException) (unchecked exception) we generally do not handle, when such an exception occurs when the program is taken over by the virtual machine. For example, we have never dealt with nullpointerexception.

1.3Java Exception Handling

When there is a runtime exception, the program will always throw up the exception until the processing code is encountered, if there is no catch block processing, to the top, if it is multi-threaded there is thread.run () thrown, if not multithreaded then by main.run() throwing. After the throw, if it is a thread, then the thread is terminated, if it is the main program, then the program is terminated. In fact, the run-time exception is also inherited from Exception, can also be used to handle the catch block, but we generally do not deal with it, that is, if the runtime exception is not catch processing, then the result is not a thread exit is the main program termination. If you do not want to terminate, then we must catch all possible run-time exceptions. If the exception data is present in the program, but it does not affect the execution of the program below, then we should discard the exception data in the catch block and log it. If, it affects the following program to run, then it is better to quit the program.

2.Java exception inheritance structure (on-line reference)

3.Java Inspection anomaly capture 3.1java exception handling mechanism

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

    • 抛出异常: When a method throws an exception with an error, the method creates an 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.

    • 捕获异常: 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.

Any Java code can throw exceptions, such as code written by itself, code from the Java Development environment package, or a Java runtime system. No matter who, you can throw an exception through the Java throw statement. Any exception thrown from a method must use a throws clause to catch the exception through a try-catch statement or try-catch-finally statement implementation.

3.2 Keyword Description

Java handles exceptions in an object-oriented manner, and Java classifies exceptions according to different types and provides a good interface. In Java, each exception is an object, which is an instance of Throwable or its subclasses. When an exception is thrown, a method throws an exception object that contains the exception information, and the method that invokes the object can catch the exception and handle the exception. Java exception handling is achieved through 5 keywords: try, catch, throw, throws, finally.

    • try: Used to specify a piece of program to prevent all exceptions
    • catch: Immediately after the try to catch the exception
    • throw: Used to explicitly throw an exception
    • throws: Used to indicate a variety of exceptions that a member function might throw: A piece of finally code that ensures that a piece of code will be executed regardless of what exception occurs.
Additional Instructions for 4.Java exceptions

When you use multiple catch statement blocks to catch an exception, you need to place the catch statement block of the parent class after the catch block of the child type to ensure that subsequent catch may be executed, otherwise the catch of the subtype will never arrive, and the Java compiler will report a compilation error. If a return statement exists in a try statement block, the code in the finally statement block is executed first before it is returned. If a statement exists in a try statement block, the System.exit(0) code of the finally statement block is not executed for long, because the System.exit(0) currently running JVM is terminated, and the program ends execution before the JVM terminates.

4.1 Test Code
@Test    public  void TestException2 () {        try {            throw new Exception ();        } catch ( Java.io.FileNotFoundException ex) {            System.out.print ("filenotfoundexception!");        } catch ( Java.io.IOException ex) {            System.out.print ("ioexception!");        } catch (Java.lang.Exception ex) {            System.out.print ("exception!");        }    }    @Test    public  void TestException1 () {/      *  try {            int s = 12/0;            System.out.print ("Try Inside");        } catch (Exception e) {            e.printstacktrace ();        } */        int s = 12/0;        System.out.print ("try Out");    }

  

Summary of Java Exceptions

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.