JAVA exception and Exception Handling Methods

Source: Internet
Author: User
Tags exit in

JAVA exception and Exception Handling Methods

Exception

This is my teacher's preference: that is to say, I will show you a picture as soon as I come up -_-

This is one:

Exception classification chart

 

Come on, there is a common exception with Chinese !!!

1: Let's talk about exceptions first:

In fact, "the problem of blocking the execution of the current method or scope" is called an exception.

There are two types of exceptions: ① compile-time exception ② runtime exception.

Let's start with "exception during compilation !!!!

That is, when the code is not run, the compiled code will pop up the syntax or other exceptions, which are called compile-time exceptions;

Besides, "an exception occurred while running "!!!

The exception is just the opposite of the exception during compilation, that is, the exception that occurs when the code is already running and the program cannot run;

2: related issues in Try-catch-finally !! (Excerpt)

Case 1:

Try {} catch () {} finally {} return;
Obviously, the program is executed in order.
Case 2:

Try {return;} catch () {} finally {} return;
The program executes the code before return in the try block (including expression operations in the return statement;
Then execute finally blocks, and finally execute return in try;
The return statement after the finally block is not executed because the program has been returned in try.
Case 3:

Try {} catch () {return;} finally {} return;
The program executes try first. If an exception occurs, execute catch Block,
If an exception occurs, execute the code before return (including the expression operation in the return statement) in catch, and then execute all the code in the finally statement,
Finally, the Code at 4 points after return. finally in the catch block is executed.
No exception: Execute try and finally return again.
Case 4:

Try {return;} catch () {}finally {return ;}
The program executes the code before return in the try block (including expression operations in the return statement;
Then execute the finally block. Because the finally block contains return, exit in advance.
Case 5:

Try {} catch () {return;} finally {return ;}
The code before the return (including expression operations in the return statement) in the catch block is executed by the program;
Then execute the finally block. Because the finally block contains return, exit in advance.
Case 6:

Try {return;} catch () {return;} finally {return ;}
The program executes the code before return in the try block (including expression operations in the return statement;
Exception: Execute the code before return (including expression operations in the return statement) in the catch Block;
Then execute the finally block. Because the finally block contains return, exit in advance.
No exception: Execute the finally block again. Because the finally block contains return, exit in advance.

(No matter what an exception occurs, the finally block code will be executed. But, do you see this!

Well, when System. exit (1) is called in the catach block, the finally block will not be executed;

 

It is better not to have a return value in the finally block, otherwise the program will exit in advance)

 

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.