public class Example {
public static void main (String ... args ) {
System .out .println (testFinally ());
}
public static String testFinally () {
try {
return "try" ;
} catch (Exception e ) {
return "catch" ;
} finally {
System .out .println ("finally" );
}
}
}
$ javac Example.java && java Example
finally
try
public class Example {
public static void main (String ... args ) {
System .out .println (testFinally ());
}
public static String testFinally () {
try {
throw new NullPointerException ("NPE" );
} catch (Exception e ) {
return "catch" ;
} finally {
System .out .println ("finally" );
}
}
}
$ javac Example.java && java Example
finally
catch
try-finally -> finally -> try-catch
public class Example {
public static void main (String ... args ) {
System .out .println (testFinally ());
}
public static String testFinally () {
try {
try {
throw new NullPointerException ("NPE" );
} catch (Exception e ) {
return "try-catch" ;
} finally {
System .out .println ("try-finally" );
}
} catch (Exception e ) {
return "catch" ;
} finally {
System .out .println ("finally" );
}
}
}
$ javac Example .java && java Example
try -finally
finally
try -catch