Try catch finally usage

Source: Internet
Author: User
Tags try catch

1. If the catch fails to throw an exception, the execution of the function will end. The code after the exception location will not be executed.

Public class test {</P> <p>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> system. out. println ("--- main () Start --->"); </P> <p> Hello (); </P> <p> testseq (); </P> <p> system. out. println ("--- main () end <---"); <br/>}</P> <p> Public static void Hello () {<br/> try {<br/> method (); <br/>} catch (nullpointerexception e) {<br/> E. printstacktrace (); <br/> system. out. println ("nullpointerexception occured. "); <br/>}finally {<br/> system. out. println ("finally... "); <br/>}</P> <p> system. out. println ("after hello"); <br/>}</P> <p> Public static void method () {<br/> // throw new nullpointerexception (); <br/> throw new arithmeticexception (); <br/>}</P> <p> Public static string testseq () {<br/> string result = ""; <br/> int I = 0; </P> <p> try {<br/> I = 7/I; <br/> system. out. println ("in try... "); <br/>}catch (nullpointerexception e) {<br/> E. printstacktrace (); <br/> system. out. println ("in catch... "); <br/>}finally {<br/> system. out. println ("in finally .... "); <br/>}< br/> system. out. println ("after... "); </P> <p> return result; <br/>}< br/>}

 

Output result:

--- Main () Start --->
Finally...
Exception in thread "Main" Java. Lang. arithmeticexception
At com. Min. Test. Method (test. Java: 35)
At com. Min. Test. Hello (test. Java: 22)
At com. Min. Test. Main (test. Java: 13)

 

2. Catch the thrown exception, and continue to execute this function. The code after the thrown exception location is also executed.

Package COM. min; </P> <p> public class test {</P> <p>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> system. out. println ("--- main () Start --->"); </P> <p> Hello (); </P> <p> testseq (); </P> <p> system. out. println ("--- main () end <---"); <br/>}</P> <p> Public static void Hello () {<br/> try {<br/> method (); <br/>} catch (nullpointerexception e) {<br/> E. printstacktrace (); <br/> system. out. println ("nullpointerexception occured. "); <br/>}finally {<br/> system. out. println ("finally... "); <br/>}</P> <p> system. out. println ("after hello"); <br/>}</P> <p> Public static void method () {<br/> throw new nullpointerexception (); <br/> // throw new arithmeticexception (); <br/>}</P> <p> Public static string testseq () {<br/> string result = ""; <br/> int I = 0; </P> <p> try {<br/> I = 7/I; <br/> system. out. println ("in try... "); <br/>}catch (nullpointerexception e) {<br/> E. printstacktrace (); <br/> system. out. println ("in catch... "); <br/>}finally {<br/> system. out. println ("in finally .... "); <br/>}< br/> system. out. println ("after... "); </P> <p> return result; <br/>}< br/>}

Output result:

--- Main () Start --->
Java. Lang. nullpointerexception
At com. Min. Test. Method (test. Java: 34)
At com. Min. Test. Hello (test. Java: 22)
At com. Min. Test. Main (test. Java: 13)
Nullpointerexception occured.
Finally...
After hello
In finally ....
Exception in thread "Main" Java. Lang. arithmeticexception:/by zero
At com. Min. Test. testseq (test. Java: 43)
At com. Min. Test. Main (test. Java: 15)

 

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.