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
7
votes
2 answers

Adding return in finally hides the exception

I have the following code public static void nocatch() { try { throw new Exception(); } finally { } } Which gives the error Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled…
codeMan
  • 5,730
  • 3
  • 27
  • 51
7
votes
4 answers

What's the difference between code inside finally block and code after finally block?

I was wondering what's the difference between code inside finally block and code after finally block
pete
  • 1,878
  • 2
  • 23
  • 43
7
votes
5 answers

error while using try with resources in Java

I have this method where I am using try with resources of Java SE 7. private void generateSecretWord(String filename){ try (FileReader files = new FileReader(filename)){ Scanner input = new Scanner(files); String…
brain storm
  • 30,124
  • 69
  • 225
  • 393
7
votes
5 answers

System.exit() results unexecutable finally block

I am working on My application's under maintanace module try { if (isUndermaintanace) { System.exit(1); } else { prepareResources(); } } catch (Exception e) { printStack(e); } finally { …
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
6
votes
4 answers

try-catch-finally idiom in smalltalk

How do you realize a try-catch-finally idiom in smalltalk? I see there is on:do: and ensure:, but there isn't on:do:ensure:. I must be missing something.
milan
  • 2,355
  • 2
  • 23
  • 38
6
votes
5 answers

Does ThreadAbortException still enforce executing the code in finally (try/catch) section?

I have a System.Timers.Timer timer which it's AutoReset is set to false. I use a try/finally to insure I Start the timer at the end of it's callback (I use the timer this way to prevent overlapping of callback execution). Code: // inside timer call…
Xaqron
  • 29,931
  • 42
  • 140
  • 205
6
votes
2 answers

Fastest `finally` for C++

C++ so far (unfortunately) doesn't support finally clause for a try statement. This leads to speculations on how to release resources. After studying the question on the internet, although I found some solutions, I didn't get clear about their…
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
6
votes
4 answers

Returning from function through catch block, what happens to finally block?

I've try catch finally block and if some exception occurs I'll return from the catch block, so finally block is still executed, if so, when? Before return or after return? Is this the right practice? try { // do something } catch (Exception) { …
JPReddy
  • 63,233
  • 16
  • 64
  • 93
6
votes
2 answers

Does finally ensure some code gets run atomically, no matter what?

Assume I'm going to write a Python script that catches the KeyboardInterrupt exception to be able to get terminated by the user using Ctrl+C safely However, I can't put all critical actions (like file writes) into the catch block because it relies…
6
votes
2 answers

What is the best practice in C# to propagate an exception thrown in a finally block without losing an exception from a catch block?

When an exception is possible to be thrown in a finally block how to propagate both exceptions - from catch and from finally? As a possible solution - using an AggregateException: internal class MyClass { public void Do() { Exception…
Sergey S
  • 889
  • 12
  • 18
6
votes
4 answers

When does a finally block execute if the catch block contains a continue statement?

I have some code that looks like this: foreach(var obj in collection) { try { // WriteToFile returns the name of the written file string filename = WriteToFile(obj); SendFileToExternalAPI(filename); } catch (…
Hydrargyrum
  • 3,378
  • 4
  • 27
  • 41
5
votes
11 answers

Formatting of hard to read try..catch..finally blocks?

How are you formatting your try..catch.finally blocks? Especially when only wrapping it around a small amount of code, it blows everything and makes code pretty unreadable and unsightly in my opinion. Such as: try { MyService service = new…
Alex
  • 75,813
  • 86
  • 255
  • 348
5
votes
2 answers

finally block in c#

Possible Duplicate: Finally Block Not Running?? I have a question regarding finally block in c#. I wrote a small sample code: public class MyType { public void foo() { try { Console.WriteLine("Throw…
meem
  • 61
  • 3
5
votes
1 answer

What is wrong with this Java Puzzlers piece of code?

In new, third edition of Effective Java Joshua Bloch mentions piece of code from Java Puzzlers (it's about closing resources in try-finally): For starters, I got it wrong on page 88 of Java Puzzlers, and no one noticed for years. In fact,…
ctomek
  • 1,696
  • 16
  • 35
5
votes
3 answers

String returns in finally and try block

I came across this question and I am unable to understand the reason of the output it is giving. The program is: public static String method(){ String s = new String(); try { s = "return value from try block"; return s; …
Vikas
  • 107
  • 1
  • 10