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)