A try-catch-finally sentence problem for Java exception capture

Source: Internet
Author: User

Today, learning the try-catch-finally statement, originally felt quite simple, easy to understand. Searched for a related type of question. As a result confidence was poured in the basin cold water. Mark the question first and go out to get some air.

1  Public classTestEx {2     Private intC;3 4      PublicTestEx () {5     }6 7@SuppressWarnings ("Finally")8     BooleanTestEx ()throwsException {9         BooleanRET =true;Ten         Try { OneRET =testEx1 (); A}Catch(Exception e) { -System.out.println ("testEx, catch Exception"); -RET =false; the             Throwe; -}finally { -System.out.println ("testEx, finally; return value= "+ret); -             returnret; +         } -     } +  A@SuppressWarnings ("Finally") at     BooleanTESTEX1 ()throwsException { -         BooleanRET =true; -         Try { -RET =testEx2 (); -             if(!ret) { -                 return false; in             } -System.out.println ("testEx1, at the end of Try"); to             returnret; +}Catch(Exception e) { -System.out.println ("testEx1, catch Exception"); theRET =false; *             Throwe; $}finally {Panax NotoginsengSystem.out.println ("testEx1, finally; return value= "+ret); -             returnret; the         } +     } A  the@SuppressWarnings ("Finally") +     BooleanTESTEX2 ()throwsException { -         BooleanRET =true; $         Try { $             intb = 12; -              for(inti = 2; I >=-2; i--) { -SETC (b/i); theSystem.out.println ("i=" +i); -             }Wuyi             return true; the}Catch(Exception e) { -System.out.println ("testEx2, catch Exception"); WuRET =false; -             Throwe; About}finally { $System.out.println ("testEx2, finally; return value= "+ret); -             returnret; -         } -     } A  +      Public Static voidMain (string[] args) { theTestEx TestException1 =NewTestEx (); -         Try { $ Testexception1.testex (); the}Catch(Exception e) { the e.printstacktrace (); the         } the     } -  in      Public intGetC () { the         returnC; the     } About  the      Public voidSETC (intc) { the          This. C =C; the     } +}

Output:

After reading it, I was thinking,

1.Java exception handling mechanism, do I really understand?

2.Java exception handling, do I really have the control?

How do I handle return in a 3.catch body?

What do you do with return in a 4.finally body?

5. What happens when a return is encountered in the catch and finally body?

6. Is there a system.exit ()? What do you do when you meet it??

7. Just know that throws is not the perfect strategy, but also to continue to understand AH. 2017-06-1918:21:08

A try-catch-finally sentence problem for Java exception capture

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.