Java exception Handling (Exception handling)

Source: Internet
Author: User

1. Never return in a finally statement.

If you are in return a block then any finally s that Throwable aren ' t caught would be completely lost.

e.g.

1 //"done!" would be a print out, but there is no "Got it."2  Public classTest {3    Public Static voidMain (string[] args) {4     Try {5 dosomething ();6System.out.println ("done!");7}Catch(RuntimeException e) {8System.out.println ("Got it.");9     }Ten   } One    A    Public Static voiddosomething () { -     Try { -       Throw Newruntimeexception (); the}finally { -       return; -     } -   } +}

2. If return in try/catch, changes in finally would not affect return value.

finally Black run before return, but the return value would be copied.

1 //print "123"2  Public classTest {3      Public Static voidMain (string[] args) {4 System.out.println (getString ());5     }6           7      Public StaticString getString () {8String str = "123";9         Try {Ten             Throw NewException (); One}Catch(Exception e) { A              return  str;  -}finally { -str = "456"; the         } -     } -}

Java exception Handling (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.