From: gumartinm Date: Sat, 17 Nov 2012 19:18:39 +0000 (+0100) Subject: The right indentation in TryCatchFinally example X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=c49c1ee9b68426c34071df7f0fe45c40e3c31a4a;p=JavaForFun The right indentation in TryCatchFinally example --- diff --git a/Allgemeines/TryCatchFinally/src/de/example/tryfinally/Main.java b/Allgemeines/TryCatchFinally/src/de/example/tryfinally/Main.java index 49fad84..ef6df85 100644 --- a/Allgemeines/TryCatchFinally/src/de/example/tryfinally/Main.java +++ b/Allgemeines/TryCatchFinally/src/de/example/tryfinally/Main.java @@ -1,80 +1,81 @@ package de.example.tryfinally; public class Main { - static String result; - - public static void main(String[] args) { - System.out.println("Calling go"); - String st = returnAndFinally(); + static String result; + + public static void main(final String[] args) { + System.out.println("Calling go"); + final String st = returnAndFinally(); System.out.println("Back from go: " + st); System.out.println("Surprise!! " + result); - + try { - System.out.println(noCatchAndFinally()); - } catch (Exception e) { - System.out.println("Second catch."); - e.printStackTrace(); - } - } + System.out.println(noCatchAndFinally()); + } catch (final Exception e) { + System.out.println("Second catch."); + e.printStackTrace(); + } + } public static String returnAndFinally() { - result = "NOK"; - + result = "NOK"; + try { messingAround(); return result; } - catch (Exception ex) { + catch (final Exception ex) { System.out.println("Entered catch: " + result); result = "OK"; //This is the return statement that this method will execute in case of any Exception class or subclass is thrown. return result; } finally { - //You will not see FINALLY in the "Back from go:" statement. - //From stackoverflow: - //http://stackoverflow.com/questions/421797/what-really-happens-in-a-try-return-x-finally-x-null-statement. - // * Code before return statement is executed - // * Expression in return statement is evaluated - // * finally block is executed - // * Result evaluated in step 2 is returned + //You will not see FINALLY in the "Back from go:" statement. + //From stackoverflow: + //http://stackoverflow.com/questions/421797/what-really-happens-in-a-try-return-x-finally-x-null-statement. + // * Code before return statement is executed + // * Expression in return statement is evaluated + // * finally block is executed + // * Result evaluated in step 2 is returned - result = "FINALLY"; + result = "FINALLY"; System.out.println("Entered finally: " + result); - //NEVER USE return in finally block because you can loose the stack if some exception is thrown from the try block!!! + //NEVER USE return in finally block because you can loose the stack if some exception is thrown from the try block!!! //You finish with this return instead of the previous one. //http://stackoverflow.com/questions/48088/returning-from-a-finally-block-in-java //return result; } } - + public static void messingAround() throws Exception { - throw(new Exception()); + throw(new Exception()); } - - + + public static String noCatchAndFinally() throws Exception { - try { - messingAround(); - return "You will not see me"; - } - finally { - try - { - //Catching this exception does not swallow the previous one (if there was one) - messingAroundReturns(); - } - catch (Exception e) { - System.out.println("First catch."); - e.printStackTrace(); - } - //NEVER USE return in finally block because you can loose the stack if some exception is thrown from the try block!!! + try { + messingAround(); + return "You will not see me"; + } + finally { + try + { + //Catching this exception does not swallow the previous one (if there was one) + messingAroundReturns(); + } + catch (final Exception e) { + System.out.println("First catch."); + e.printStackTrace(); + } + //NEVER USE return in finally block because you can loose the stack if some exception is thrown from the try block!!! //http://stackoverflow.com/questions/48088/returning-from-a-finally-block-in-java - return "Do not do this. You are loosing the exception thrown by messingAround method!!!"; - } + // return + // "Do not do this. You are loosing the exception thrown by messingAround method!!!"; + } } - + public static void messingAroundReturns() throws Exception { - throw(new Exception()); + throw(new Exception()); } }