Questions tagged [finally]

Questions related to the finally block in try-catch construct.

278 questions
57
votes
7 answers

Multiple returns: Which one sets the final return value?

Given this code: String test() { try { return "1"; } finally { return "2"; } } Do the language specifications define the return value of a call to test()? In other words: Is it always the same in every JVM? In the Sun…
Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329
47
votes
3 answers

Angular 6 / Rxjs - how to basics: observables success, error, finally

I'm building an architecture on latest Angular 6 and coming from AngularJS there's something I can't make peace about: the basic processing of an HTTP request. So, for the sake of the question, let's say I…
Simon Peyou
  • 693
  • 1
  • 6
  • 13
46
votes
6 answers

What is the benefit to use "finally" after try-catch block in java?

The "finally" block is always executed when the try-catch ends, either in case of exception or not. But also every line of code outside and after the try-catch is always executed. So, why should I use the finally statement? Example: try { …
Aarath
  • 481
  • 1
  • 4
  • 4
43
votes
7 answers

Why is `continue` not allowed in a `finally` clause in Python?

The following code raises a syntax error: >>> for i in range(10): ... print i ... try: ... pass ... finally: ... continue ... print i ... File "", line 6 SyntaxError: 'continue' not supported inside 'finally'…
ElenaT
  • 2,600
  • 4
  • 24
  • 41
43
votes
9 answers

'Finally' equivalent for If/Elif statements in Python

Does Python have a finally equivalent for its if/else statements, similar to its try/except/finally statements? Something that would allow us to simplify this: if condition1: do stuff clean up elif condition2: do stuff …
nobillygreen
  • 1,548
  • 5
  • 19
  • 27
40
votes
2 answers

Are resources closed before or after the finally?

In Java 7's try-with-resources, I don't know which order the finally block and the auto-closing happens. What's the order? BaseResource b = new BaseResource(); // not auto-closeable; must be stop'ed try(AdvancedResource a = new AdvancedResource(b))…
djechlin
  • 59,258
  • 35
  • 162
  • 290
38
votes
8 answers

Determine if executing in finally block due to exception being thrown

Is it possible to determine if code is currently executing in the context of a finally handler as a result of an exception being thrown? I'm rather fond of using the IDisposable pattern to implement entry/exit scoping functionality, but one concern…
Dan Bryant
  • 27,329
  • 4
  • 56
  • 102
36
votes
8 answers

java try finally block to close stream

I want to close my stream in the finally block, but it throws an IOException so it seems like I have to nest another try block in my finally block in order to close the stream. Is that the right way to do it? It seems a bit clunky. Here's the code: …
jhlu87
  • 3,999
  • 8
  • 38
  • 48
33
votes
3 answers

java: try finally blocks execution

I am confused about the try-finally execution when there exists return; in the try block. In my understanding, the finally block will always be executed, i.e. before returning to the calling method. While considering the following simple…
yifei
  • 561
  • 5
  • 18
33
votes
10 answers

What is the purpose of "finally" in try/catch/finally

The syntax will change from language to language, but this is a general question. What is the difference between this.... try { Console.WriteLine("Executing the try statement."); throw new NullReferenceException(); } catch…
Maxx
  • 3,925
  • 8
  • 33
  • 38
30
votes
6 answers

Why doesn't C# have support for first pass exception filtering?

Note: this is not a duplicate of Jeff's question. That question asked "Is an equivalent?" I know there isn't, and I want to know why! The reason I ask is that I've only just become clear on how important it is, and the conclusion seems very strange…
Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
28
votes
4 answers

return eats exception

I found the following behavior at least weird: def errors(): try: ErrorErrorError finally: return 10 print errors() # prints: 10 # It should raise: NameError: name 'ErrorErrorError' is not defined The exception disappears…
nosklo
  • 217,122
  • 57
  • 293
  • 297
28
votes
2 answers

Correctly implement finally block using C++ lambda

I want to implement a finally block in my C++ program, and the language certainly has the tools to do it if not a native facility. I was wondering what the best way to do this is?
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
27
votes
5 answers

try-catch-finally with return after it

I know how try, catch & finally work (for most part), but I have one thing I was wondering: what happens with a return statement after a try-catch-finally, while we already had a return in the try (or catch)? For example: public boolean…
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
26
votes
5 answers

Curious C# using statement expansion

I've run ildasm to find that this: using(Simple simp = new Simple()) { Console.WriteLine("here"); } generates IL code that is equivalent to this: Simple simp = new Simple(); try { Console.WriteLine("here"); …
Matthew
  • 261
  • 3
  • 3
1
2
3
18 19