Questions tagged [try-catch-finally]

A common usage of catch and finally together is to obtain and use resources in a try block, deal with exceptional circumstances in a catch block, and release the resources in the finally block.

435 questions
9
votes
4 answers

break statement in finally clause java

public class FinallyTest { static int i=0; public static void main(String a[]){ while(true){ try{ i=i+1; return; }finally{ i=i+1; break; …
Shwetanka
  • 4,976
  • 11
  • 44
  • 68
9
votes
1 answer

Is using a lambda a safe, correct, and equivalent workaround for classes that do not implement AutoCloseable?

Background: I use the Java class InitialDirContext to access LDAP directories. Unfortunately, it does not implement interface AutoCloseable, so it cannot be used in try-with-resources blocks. Here is the original code I wrote: (inspired by this…
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
9
votes
3 answers

Immediately rethrowing in catch block and using finally

I have a wrapper responsible for logging operations, named OperationWrapper. Its structure is simple and as follows: public void runOperation(Operation o) throws Exception{ logOperationStarted(); o.execute(); …
Shahar
  • 478
  • 5
  • 17
9
votes
2 answers

When and why can `finally` be useful?

PHP 5.5 has implemented finally to try-catch. My doubt is: when exactly try-catch-finally that might be more helpful than just I write below try-catch? Example, difference between: try { something(); } catch(Exception $e) { other(); } finally {…
David Rodrigues
  • 12,041
  • 16
  • 62
  • 90
9
votes
5 answers

Difference between try{..}catch{...} with finally and without it

What is the difference between code like this: string path = @"c:\users\public\test.txt"; System.IO.StreamReader file = new System.IO.StreamReader(path); char[] buffer = new char[10]; try { file.ReadBlock(buffer, index, buffer.Length); } catch…
Kuba Matjanowski
  • 350
  • 3
  • 14
8
votes
3 answers

Why code in finally block doesn't execute?

It seems that finally block doesn't execute if it other than main thread execute code. Is it possible to force execute finally in that case? Environment: VS 2010, .Net Framework 4.0.3 class Program { static void Main(string[] args) { …
AndreyR
  • 294
  • 4
  • 15
8
votes
3 answers

Understanding try catch finally with return and value that it returns

I have the following piece of code. public static void main(String[] args) { System.out.println(returnString()); } private static String returnString(){ try { System.out.println("Executing try"); return "Return try value"; …
StackFlowed
  • 6,664
  • 1
  • 29
  • 45
8
votes
10 answers

Use case for try-catch-finally with both catch and finally

I understand how try-catch works and how try-finally works, but I find myself using those (usually) in two completely different scenarios: try-finally (or using in C# and VB) is mostly used around some medium-sized code block that uses some…
Heinzi
  • 167,459
  • 57
  • 363
  • 519
8
votes
3 answers

Exiting an application gracefully?

I have an application with a well defined Try/Catch/Finally chain that exits and executes the finally block just fine under normal conditions, however when someone prematurely hits the red X in the GUI, the program fully exists (code = 0) and the…
E.S.
  • 2,733
  • 6
  • 36
  • 71
8
votes
11 answers

Is finally block really necessary for the clean up code (like closing streams)?

I am very confused as to why do I need to need to put the clean-up code like closing streams in a finally block. I've read that the code in finally block will run no matter what (whether there's an exception); and after the finally block runs, the…
Ankit
  • 609
  • 2
  • 10
  • 26
8
votes
4 answers

do I need to surround fileInputStream.close with a try/catch/finally block? How is it done?

I have the following Java Class that does one thing, fires out values from config.properties. When it comes time to close the fileInputStream, I think I read on Wikipedia that it is good to have it in a finally block. Because it honestly works just…
JoJo
  • 4,643
  • 9
  • 42
  • 65
7
votes
1 answer

Is finally "out of scope" in a try/catch block

Is there a way to access val's created in a try/catch block within the finally block ? or is the finally block out of scope. def myTryCatch: Either[Exception, String] = { try { val w = runOrFailWithException("Please work...") Right(w) }…
Peter Lerche
  • 469
  • 4
  • 6
7
votes
4 answers

Unit testing finally blocks in Java 6

While reviewing my code coverage i noticed a lot of Unit tests fail to check finally blocks which try to close open InputStreams in finally blocks. One Example excerpt is: try { f = new BufferedInputStream(new FileInputStream(source)); …
fyr
  • 20,227
  • 7
  • 37
  • 53
7
votes
2 answers

Yield return from a try/catch block

As Eric Lippert described in this article, yield return is not allowed within try/catch clauses. Is there a nice way I could get something like this, without having to write my own IEnumerator by hand: public IEnumerable GetData() { var…
vgru
  • 49,838
  • 16
  • 120
  • 201
7
votes
1 answer

How to handle throw exceptions inside finally block in java

In java, it is not recommended to throw exceptions inside finally section in try-chatch block due to hide the propagation of any unhandled throwable which was thrown in the try or catch block. This practice is a blocker level violation according to…
Hiran Perera
  • 736
  • 1
  • 5
  • 18