Questions tagged [try-finally]

try-finally is a clause used to define a block of code which may throw an exception along with instructions to execute regardless of whether an exception occurs or not.

References

182 questions
527
votes
18 answers

Why do we need the "finally" clause in Python?

I am not sure why we need finally in try...except...finally statements. In my opinion, this code block try: run_code1() except TypeError: run_code2() other_code() is the same with this one using finally: try: run_code1() except…
RNA
  • 146,987
  • 15
  • 52
  • 70
334
votes
6 answers

Try-finally block prevents StackOverflowError

Take a look at the following two methods: public static void foo() { try { foo(); } finally { foo(); } } public static void bar() { bar(); } Running bar() clearly results in a StackOverflowError, but running foo()…
arshajii
  • 127,459
  • 24
  • 238
  • 287
155
votes
9 answers

Why does a return in `finally` override `try`?

How does a return statement inside a try/catch block work? function example() { try { return true; } finally { return false; } } I'm expecting the output of this function to be true, but instead it is false!
bonfo
  • 2,110
  • 3
  • 17
  • 14
148
votes
7 answers

Why does changing the returned variable in a finally block not change the return value?

I have a simple Java class as shown below: public class Test { private String s; public String foo() { try { s = "dev"; return s; } finally { s = "override variable s"; …
Devendra
  • 1,864
  • 6
  • 29
  • 49
132
votes
11 answers

Java Try Catch Finally blocks without Catch

I'm reviewing some new code. The program has a try and a finally block only. Since the catch block is excluded, how does the try block work if it encounters an exception or anything throwable? Does it just go directly to the finally block?
NullPointer0x00
  • 1,808
  • 3
  • 18
  • 20
117
votes
4 answers

Python try finally block returns

There is the interesting code below: def func1(): try: return 1 finally: return 2 def func2(): try: raise ValueError() except: return 1 finally: return 3 func1() func2() Could please…
skybobbi
  • 1,486
  • 2
  • 10
  • 7
110
votes
11 answers

Difference between try-finally and try-catch

What's the difference between try { fooBar(); } finally { barFoo(); } and try { fooBar(); } catch(Throwable throwable) { barFoo(throwable); // Does something with throwable, logs it, or handles it. } I like the second version better…
Vijay Kotari
  • 1,945
  • 3
  • 15
  • 16
79
votes
6 answers

Overhead of try/finally in C#?

We've seen plenty of questions about when and why to use try/catch and try/catch/finally. And I know there's definitely a use case for try/finally (especially since it is the way the using statement is implemented). We've also seen questions about…
Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
47
votes
6 answers

How to determine if an exception was raised once you're in the finally block?

Is it possible to tell if there was an exception once you're in the finally clause? Something like: try: funky code finally: if ???: print('the funky code raised') I'm looking to make something like this more DRY: try: funky…
wim
  • 338,267
  • 99
  • 616
  • 750
36
votes
5 answers

What happens if both catch and finally blocks throw exception?

What happens if both catch and finally blocks throw exception?
Arthur
  • 674
  • 1
  • 8
  • 14
34
votes
2 answers

Why does try..finally block not register the original exception as suppressed?

With the following code: try { throw new RuntimeException ("main"); } finally { throw new RuntimeException ("finally"); } I get this result: Exception in thread "main" java.lang.RuntimeException: finally at…
user319799
33
votes
3 answers

Python: Using continue in a try-finally statement in a loop

Will the following code: while True: try: print("waiting for 10 seconds...") continue print("never show this") finally: time.sleep(10) Always print the message "waiting for 10 seconds...", sleep for 10…
Andres Riofrio
  • 9,851
  • 7
  • 40
  • 60
30
votes
7 answers

How to correctly write Try..Finally..Except statements?

Take the following code as a sample: procedure TForm1.Button1Click(Sender: TObject); var Obj: TSomeObject; begin Screen.Cursor:= crHourGlass; Obj:= TSomeObject.Create; try // do something finally Obj.Free; end; …
user741875
29
votes
4 answers

Does the statements in the Finally block still execute in this piece of code ?

Will finally block execute? if I pass exit; ? procedure someProc; begin Try Exit; finally do_something; end; end;
jmp
  • 2,456
  • 3
  • 30
  • 47
23
votes
2 answers

Why does the Java Compiler copy finally blocks?

When compiling the following code with a simple try/finally block, the Java Compiler produces the output below (viewed in the ASM Bytecode Viewer): Code: try { System.out.println("Attempting to divide by zero..."); System.out.println(1 /…
Clashsoft
  • 11,553
  • 5
  • 40
  • 79
1
2 3
12 13