Questions tagged [finally]

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

278 questions
329
votes
16 answers

Does C++ support 'finally' blocks? (And what's this 'RAII' I keep hearing about?)

Does C++ support 'finally' blocks? What is the RAII idiom? What is the difference between C++'s RAII idiom and C#'s 'using' statement?
Kevin
  • 25,207
  • 17
  • 54
  • 57
185
votes
6 answers

Does 'finally' always execute in Python?

For any possible try-finally block in Python, is it guaranteed that the finally block will always be executed? For example, let’s say I return while in an except block: try: 1/0 except ZeroDivisionError: return finally: print("Does this…
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
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
124
votes
12 answers

Does a finally block always run?

Is there any condition where finally might not run in java? Thanks.
Warrior
  • 39,156
  • 44
  • 139
  • 214
120
votes
4 answers

Using python "with" statement with try-except block

Is this the right way to use the python "with" statement in combination with a try-except block?: try: with open("file", "r") as f: line = f.readline() except IOError: If it is, then considering the old way of doing…
new name
  • 15,861
  • 19
  • 68
  • 114
109
votes
6 answers

What is the point of finally in a try catch/except finally statement

I have used try-catch/except-finally variants in many languages for years, today someone asked me what is the point of finally and I couldn't answer. Basically why would you put a statement in finally instead of just putting it after the whole…
Ali
  • 18,665
  • 21
  • 103
  • 138
106
votes
11 answers

Why do we use finally blocks?

As far as I can tell, both of the following code snippets will serve the same purpose. Why have finally blocks at all? Code A: try { /* Some code */ } catch { /* Exception handling code */ } finally { /* Cleanup code */ } Code B: try { /* Some code…
Mohammad Nadeem
  • 9,134
  • 14
  • 56
  • 82
104
votes
15 answers

throws Exception in finally blocks

Is there an elegant way to handle exceptions that are thrown in finally block? For example: try { // Use the resource. } catch( Exception ex ) { // Problem with the resource. } finally { try{ resource.close(); } catch( Exception…
Paul
  • 1,757
  • 2
  • 11
  • 21
99
votes
6 answers

Writing try catch finally in shell

Is there a linux bash command like the java try catch finally? Or does the linux shell always go on? try { `executeCommandWhichCanFail` mv output } catch { mv log } finally { rm tmp }
Jetse
  • 1,706
  • 2
  • 16
  • 22
88
votes
2 answers

Try-catch-finally-return clarification

By reading all the questions already asked in this forum related to the topic above (see title), I thoroughly understand that finally gets always called. (except from System.exit and infinite loops). However, I would like to know if a return is…
Rollerball
  • 12,618
  • 23
  • 92
  • 161
85
votes
7 answers

In Java, what purpose do the keywords `final`, `finally` and `finalize` fulfil?

In Java, what purpose do the keywords final, finally and finalize fulfil?
Pat R Ellery
  • 1,696
  • 3
  • 22
  • 40
83
votes
4 answers

How to always run some code when a promise is fulfilled in Angular.js

In my Angular.js application, I'm running some asynchronous operation. Before it starts I cover the application with a modal div, then once the operation is complete, I need to remove the div, whether the operation was successful or not. Currently I…
laurent
  • 88,262
  • 77
  • 290
  • 428
60
votes
4 answers

Can we use "return" in finally block

Can we use return statement in finally block. Can this cause any problem?
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
57
votes
3 answers

C++, __try and try/catch/finally

I'm wondering a bit about C++ try/catch/finally blocks. I've seen these commands with two underscores like __try. But MVSC 2010 projects also run without the underscores. So when do you need these underscores?
martin
  • 1,099
  • 4
  • 15
  • 18
57
votes
3 answers

Javascript error handling with try .. catch .. finally

I have a suspicion that I'm using the finally block incorrectly, and that I don't understand the fundamentals of its purpose... function myFunc() { try { if (true) { throw "An error"; } } catch (e)…
nickf
  • 537,072
  • 198
  • 649
  • 721
1
2 3
18 19