Java object-oriented programming--11th chapter exception handling

Source: Internet
Author: User

1. Exception: The object that describes the error message.

When a byte-code check occurs, an exception is thrown if there is an error.

2, all the exception of the parent class is exception, the exception can be caught, can be processed.

The parent of all errors is error, which can be caught, but cannot be handled.

Throwable is the parent class for exception and error.

3, through Try{}catch (Exception e) {e.printstacktrace ();} Exceptions can be caught, where try represents a block of code that needs to be instrumented, a catch represents a capture, Exception E represents the creation of the variable e, and if an exception is thrown in the try block, the virtual machine points to the exception object with an E variable. E.printstacktrace (); Indicates that the exception information encapsulated in the exception object is printed out.

4, the order of exception execution, first execute the code in the try block, if there is an exception in the try block, then terminate the other code in the try block, into the catch block execution. Finally executes regardless of whether there is an exception.

5. Finally block is a block of code that executes regardless of whether an exception occurs. Generally used for resource cleanup work, such as: Stream shutdown, database connection shutdown, and so on. If there is a return bounce method in a try block or catch block, then the finally content is executed before the method jumps out, and finally will not be executed if there is system.exit (0) in the try block or catch block that aborts the virtual machine.

6. You can add multiple catch blocks to a try block, and when a throw exception is thrown in a try block, the first catch block that matches the exception object is executed, and the rest of the catch block is ignored and not executed. And the exception class in the added catch block cannot put the parent exception before the subclass exception.

7, exceptions are divided into two categories: compile-time exceptions (check exceptions) and run-time exceptions (non-check exceptions). In this case, the compile-time exception requires the developer to capture and process, otherwise it cannot be compiled, the runtime exception, the developer can handle, or can not handle, whether or not processing can be compiled. All run-time exceptions are runtimeexception subclasses.

8, throw is a statement indicating that an exception object is thrown.

throw new Exception ();

9. Throws is a method modifier that indicates that an exception may be thrown in the method. This way, when the method is called, the exception thrown by the method must be caught and handled.

A subclass method that overrides a parent class method cannot declare an exception that throws more than the parent class method.

Java object-oriented programming--11th chapter 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.