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