Exception handling mechanism for Java (bottom)

Source: Internet
Author: User
Tags finally block

Exceptions are caused by an error in executing the function, and the function is a layer of nested layers called to form the call stack. As a result, an error occurs in one of the multi-layered nested functions, and the rest will fail to execute. The exception tracking stack is formed when all these functions output exception information.

At the very beginning of the code where the exception occurred, called the exception throw point. The remainder of the exception that occurs because of a function exception is traced back to the bottom of the call stack. Such behavior, called, is abnormal bubbling.

The basic syntax of exception handling try-catch-finally, there are the following points to note:

1.try, catch, finally in the respective local variables can only be used locally, each other can not be interoperable.

2. Each catch block is used to handle an exception, and the run order is matched from top to bottom. If a parent-child relationship exists between the catch blocks in the same try, it should be placed on top and the parent class behind.

The throws function declares that if the code inside a method throws a check exception (checked exception), and the method does not completely handle the exception, then JAVSC requires that the throws keyword be written behind the method name, or the compilation does not pass.

Throws only throws an exception type and does not handle it, throwing an exception for the caller to handle.

Finally block: Regardless of whether there is an exception, as long as the corresponding try executes, he will execute. In programming, develop a good habit of opening a resource in try and closing the resource in finally. It is most appropriate to write only the cleanup resources in finally, and do not deal with any problems in this area. The return statement, preferably written on the last side of the function, is not written in try-catch-finally.

Well, my understanding of the anomaly, is so far, will know more, experience is slowly accumulated, come on

Exception handling mechanism for Java (bottom)

Related Article

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.