publicclasswithReturn{publicstaticvoidmain(String[]args){System.out.println(catchException());}privatestaticvoidthrowException()throwsException{thrownewException("throwing Exception");}privatestaticStringcatchException(){try{System.out.println("try block");throwException();return"try return";}catch(Exceptione){System.out.printf("catch the exception: `%s` in catch block\n",e.toString());}finally{System.out.println("finally block");}return"outside the try-catch-finally block";}}
publicclasswithReturn{publicstaticvoidmain(String[]args){System.out.println(noException());}privatestaticStringnoException(){try{System.out.println("try block");return"try return";}catch(Exceptione){System.out.printf("catch the exception: `%s` in catch block\n",e.toString());}finally{System.out.println("finally block");}return"outside the try-catch-finally block";}}
try block
catch the exception: `java.lang.Exception: throwing Exception` in catch block
finally block
Exception in thread "main" java.lang.ArithmeticException: / by zero
at tryCatchFinally.catchFinallyException.catchThrowException(catchFinallyException.java:18)
at tryCatchFinally.catchFinallyException.main(catchFinallyException.java:5)
可见,catch 块在抛出异常后终止,最后 main 线程会抛出异常,但是 finally 块仍然运行。
try block
catch the exception: `java.lang.Exception: throwing Exception` in catch block
finally block
Exception in thread "main" java.lang.ArithmeticException: / by zero
at tryCatchFinally.catchFinallyException.catchFinallyException(catchFinallyException.java:20)
at tryCatchFinally.catchFinallyException.main(catchFinallyException.java:5)
publicclasswithContinue{publicstaticvoidmain(String[]args){System.out.println(tryContinue());}privatestaticStringtryContinue(){for(inti=0;i<2;i++){try{System.out.println("try block");continue;}catch(Exceptione){System.out.printf("catch the exception: `%s` in catch block\n",e.toString());}finally{System.out.println("finally block");}}return"outside the try-catch-finally block";}}
publicclasswithContinue{publicstaticvoidmain(String[]args){System.out.println(catchContinue());}privatestaticStringcatchContinue(){for(inti=0;i<2;i++){try{System.out.println("try block");thrownewException("throwing exception");}catch(Exceptione){System.out.printf("catch the exception: `%s` in catch block\n",e.toString());continue;}finally{System.out.println("finally block");}}return"outside the try-catch-finally block";}}
publicclasswithBreak{publicstaticvoidmain(String[]args)throwsException{System.out.println(tryBreak());}privatestaticStringtryBreak(){for(inti=0;i<2;i++){try{System.out.println("try block");break;}catch(Exceptione){System.out.printf("catch the exception: `%s` in catch block\n",e.toString());}finally{System.out.println("finally block");}}return"outside the try-catch-finally block";}}
publicclasswithBreak{publicstaticvoidmain(String[]args)throwsException{System.out.println(catchBreak());}privatestaticStringcatchBreak(){for(inti=0;i<2;i++){try{System.out.println("try block");thrownewException("throwing exception");}catch(Exceptione){System.out.printf("catch the exception: `%s` in catch block\n",e.toString());break;}finally{System.out.println("finally block");}}return"outside the try-catch-finally block";}}
publicclasswithExit{publicstaticvoidmain(String[]args){System.out.println(tryExit());}privatestaticStringtryExit(){try{System.out.println("try block");System.exit(1);}catch(Exceptione){System.out.printf("catch the exception: `%s` in catch block\n",e.toString());}finally{System.out.println("finally block");}return"outside the try-catch-finally block";}}
publicclasswithExit{publicstaticvoidmain(String[]args){System.out.println(catchExit());}privatestaticvoidthrowException()throwsException{thrownewException("throwing Exception");}privatestaticStringcatchExit(){try{System.out.println("try block");throwException();}catch(Exceptione){System.out.printf("catch the exception: `%s` in catch block\n",e.toString());System.exit(1);}finally{System.out.println("finally block");}return"outside the try-catch-finally block";}}