<JAVA> Summary Record 01: Exception Handling Try-catch-finally

Source: Internet
Author: User
Tags finally block

Originally in the course of study on the abnormal handling mechanism, the results of today's CVTE School recruit online assessment, met two such problems (a first question, a oshio the penultimate), good worry Mulberry ah.

So the mend, summed up the good ~

1. Exception Handling block

1 Try{2      //statements that may produce an exception3}Catch(Exception1 e) {4     //processing statements When an exception Excertion1 occurs5}Catch(Exception2 e) {6     //processing statements When an exception Excertion2 occurs7}finally{8     //statements that are executed anyway (typically used to release some resources, etc.)9 }Ten //Period Exception Exception1 cannot be a base class for Exception2, Exception1 can be a subclass of Exception2
exception handling block mode one
1  Public void throws exception1{2     // throws an exception directly after the method name Exception1 3 }
exception handling block mode two

Mode one (catch exception) Try-catch can be nested using form nested try-catch blocks;

Mode two (throws an exception) can throw multiple exceptions, that is, you can add an exception list after throws, and the exception names are separated by commas.

The Ps:printstacktrace () method is used to track information in the print stack when an exception event occurs (Throwable object)

2. Finally clause

In any case, the statements in the finally block are executed, regardless of whether an exception occurs in the try block or if an exception is caught;

The finally clause is typically used for I/O programming, where a file closing statement is placed in a finally block.

PS:1) If no exception occurs in the try block, the statement in the finally block is executed, and the next statement is executed out of the exception handling block;

2) If a statement in the try block has an exception and is caught by a catch block, the other statements in the try block are skipped to execute the catch block and the finally clause directly;

3) If there is a statement in the try block with an exception, but not caught by any catch block, the other statements in the try block are skipped to execute the finally clause directly;

4) Even if there is a return statement before the finally block is reached, the finally block is still executed, but before the finally clause is executed, the return value of the expression is completed (and saved) in the following order:

The Try statement (part of the code before return)---evaluates the value returned by return---finally---executes the return statement in the try

(Contains the return execution order summary, detailed can see http://blog.csdn.net/kavensu/article/details/8067850 "3Q, the small woman here thanked ~")

<JAVA> Summary Record 01: Exception Handling Try-catch-finally

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.