Why use finally instead of catch code

Why do this

} catch (SQLException sqle) { sqle.printStackTrace(); } finally { cs.close(); rs.close(); } 

Instead of this

 } catch (SQLException sqle) { sqle.printStackTrace(); } rs.close(); cs.close(); 
+55
java try-catch-finally
Jan 14 2018-11-11T00:
source share
14 answers

Because if the exception gets thrown , the code after the try block is executed if the exception is not caught. The finally block is always executed no matter what happens inside your try block.

+61
Jan 14 2018-11-11T00:
source share

Look at your catch block - it's going to throw a DAOException . Therefore, statements after your catch block will not be executed even in the sample of your choice. What you showed (wrapping one exception in another) is one common pattern, but another possibility is that the catch block β€œaccidentally” throws an exception, for example. because one of the challenges that it calls fails.

In addition, there may be other exceptions that you do not catch - either because you declared that the method throws them, or because they are unchecked exceptions. Do you really want a resource leak because an IllegalArgumentException was thrown somewhere?

+21
Jan 14 2018-11-11T00:
source share

Because if an exception is thrown,

  • The code in the finally clause will execute as the exception is thrown out, even if the exception cancels the rest of the method execution;

  • The code after the try / catch block is not executed if the exception is not caught by the catch block and is not shown again.

+8
Jan 14 2018-11-11T00:
source share

Because it ensures that the material in the finally block is executed. Material after catch cannot be executed, say, for example, there is one more exception in the catch block, which is very possible. Or you just do what you do and throw an exception that wraps the original exception.

+6
Jan 14 2018-11-11T00:
source share

The finally keyword ensures that the code is executed. In the bottom example, close statements are NOT executed. In the upper example, they are executed (what you need!)

+2
Jan 14 2018-11-11T00:
source share

In your second approach, the close statements will not be executed, since it already left the method.

+1
Jan 14 2018-11-11T00:
source share

If you catch all the errors, there should be no difference, otherwise only the code inside the finally block is executed, because the sequence of code execution: finally code β†’ error throw β†’ code after catch therefore, as soon as your code throws out any unhandled error, only code code like usually works as expected.

+1
09 Feb '17 at 3:08 on
source share

This is a way to avoid resource leaks.

0
Jan 14 2018-11-11T00:
source share

The code in the finally block will be called before the exception is thrown from the catch block. This ensures that the cleanup code that you put in the finally block is called. Code outside the finally block will not run.

0
Jan 14 2018-11-11T00:
source share
0
Jan 14 2018-11-11T00:
source share

believe that catch can throw an exception for higher-level functions in the call stack. This will end the call before throwing the exception to the upper level.

0
Sep 12 '12 at 10:18
source share

In http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html this is misleading (and maybe a question arose):

The try block of the writeList method that you've been working with here opens a PrintWriter. The program should close that stream before exiting the writeList method. This poses a somewhat complicated problem because writeList try block can exit in one of three ways.

 1. The new FileWriter statement fails and throws an IOException. 2. The list.get(i) statement fails and throws an IndexOutOfBoundsException. 3. Everything succeeds and the try block exits normally. 

The 4th way is missing (exception other than IOException and IndexOutOfBoundsException ). The code shown on the previous page only catches (1) and (2) before resorting to finally .

I am also new to Java and asked the same question before finding this article. Hidden memory, as a rule, is more attached to examples than to theory.

0
Nov 05 '14 at 22:52
source share

The finally block may not always execute; consider the following code.

 public class Tester { public static void main(String[] args) { try { System.out.println("The main method has run"); System.exit(1); } catch (Exception e) { e.printStackTrace(); } finally { System.out.println("The finally block has run"); } } } 

In your case, I would suggest wrapping the code inside the finally block in try / catch, as this code is likely to throw an exception.

  } catch (SQLException sqle) { sqle.printStackTrace(); } finally { try { cs.close(); rs.close(); } catch (Exception e) { //handle new exception here } 
0
Dec 09 '16 at 6:01
source share

According to HeadFirst Java, the finally block will work even if the try or catch block has a return statement. The flow goes to the end, and then back to return.

0
Aug 23 '17 at 11:31 on
source share



All Articles