Package com.smbea.demo.trycatchfinally;/** * Java finally usage * @author hapday * @ February 5, 2017 @ Morning 12:21:16 */public class Tryc Atchfinallydemo {public static void main (string[] args) {hasexception (); System.out.println ("\ n —————————————————————————————————————————————————————————————————— \ n"); noneexception (); System.out.println ("\ n —————————————————————————————————————————————————————————————————— \ n"); System.out.println (Hasreturnandnoneexception ()); System.out.println ("\ n —————————————————————————————————————————————————————————————————— \ n"); System.out.println (Hasreturnandhasexception ());} /** * Has an exception occurs */public static void Hasexception () {int divisor = 10;int Dividend = 0;try {System.out.println (divisor + "/" + dividend + "=" + (divisor/dividend));} catch (Exception e) {System.out.println ("The code here only executes after an exception has occurred ..."); E.printstacktrace (); finally {System.out.println ("The code here always executes, regardless of whether there is an exception or if a return statement appears in the try block, is unaffected ...");} /** * No exception occurs */public static void Noneexception () {int divisor = 10;int Dividend = 10;try {System.out.println (divisor + "/" + dividend + "=" + (divisor/dividend));} catch (Exception e) {System.out.println ("The code here only executes after an exception has occurred ..."); E.printstacktrace (); finally {System.out.println ("The code here always executes, regardless of whether there is an exception or if a return statement appears in the try block, is unaffected ...");} /** * Try block with return statement but no exception * @return */@SuppressWarnings ("finally") public static int hasreturnandnoneexception () {int di Visor = 10;int Dividend = 10;try {System.out.println (divisor + "/" + dividend + "=" + (divisor/dividend));d ivisor *= 2; System.out.println ("divisor =" + divisor); return divisor;} catch (Exception e) {System.out.println ("The code here only executes after an exception has occurred ..."); E.printstacktrace (); finally {System.out.println ("The code here always executes, regardless of whether there is an exception or if a return statement appears in the try block, is unaffected ...");d ivisor *= 3; System.out.println ("divisor =" + divisor); return divisor;}} /** * Try block with return statement but exception * @return */@SuppressWarnings ("finally") public static int hasreturnandhasexception () {int div Isor = 10;int Dividend = 0;try {System.out.println (divisor + "/" + dividend + "=" + (divisor/dividend));d ivisor *= 2; System.out.println ("divisor =" + divisor); return divisor;} catch (Exception e) {System.out.println ("The code here only executes after an exception has occurred ..."); E.printstacktrace (); finally {System.out.println ("The code here always executes, regardless of whether there is an exception or if a return statement appears in the try block, is unaffected ...");d ivisor *= 3; System.out.println ("divisor =" + divisor); return divisor;}}}
The use of the finally Java