The finally processing effect in exceptions in Java

Source: Internet
Author: User

In the Main method

try {
System.out.println (AA ());
} catch (Exception e) {
TODO auto-generated Catch block
System.out.println ("hehe big");
E.printstacktrace ();
}
System.out.println (4444);

-----------------------------------

static int aa () throws exception{
try {
int a=4/0;
Thread.Sleep (1000);
} catch (Interruptedexception e) {
E.printstacktrace ();
return 1;
}finally{
System.out.println ("finally");
return 2;
}

}

The output results are as follows:

Finally
2
4444

Analysis: Although there is a run exception in try Java.lang.ArithmeticException, but not caught by catch, whether the program is normal operation, or throw an exception, before the first call finally (but one test is to hit the exception first, after the output finally, very surprised), F Inally is used to release resources, if finally use return, this is not the standard notation, this should not be used, finally the return of this method to lose the exception to run, the equivalent of the AA method does not throw an exception

The finally processing effect in exceptions in Java

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.