Questions tagged [finally]

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

278 questions
25
votes
10 answers

In Java, is the "finally" block guaranteed to be called (in the main method)?

I'm a Java rookie and I was wondering, if I have the following typical Java code public class MyApp { public static void main(String[] args) { try { // do stuff } catch { // handle errors } finally { // clean up…
Hanno Fietz
  • 30,799
  • 47
  • 148
  • 234
24
votes
1 answer

python try-finally

Why does the exception in foo whizz by unnoticed, but the exception in bar is raised? def foo(): try: raise Exception('foo') finally: return def bar(): try: raise Exception('bar') finally: …
wim
  • 338,267
  • 99
  • 616
  • 750
21
votes
9 answers

C# Time of finally execution

Take this code: using System; namespace OddThrow { class Program { static void Main(string[] args) { try { throw new Exception("Exception!"); } finally …
Thanatos
  • 42,585
  • 14
  • 91
  • 146
21
votes
8 answers

Finally sometimes confuse me

Today in college we talked a little bit about try, catch and finally. I got confused about these two examples: PrintWriter out = null; try { out = new PrintWriter(...); // We open file here } catch (Exception e) { e.printStackTrace(); } finally…
Miljan Rakita
  • 1,433
  • 4
  • 23
  • 44
21
votes
5 answers

What's the `finally` keyword for in PHP?

Consider these two examples
marxjohnson
  • 830
  • 7
  • 21
20
votes
5 answers

The finally block is not running on .NET 4.0, why?

Ok this is kind of a weird issue and I am hoping someone can shed some light. I have the following code: static void Main(string[] args) { try { Console.WriteLine("in try"); throw new EncoderFallbackException(); } …
jquery auth
  • 1,267
  • 1
  • 14
  • 24
19
votes
6 answers

Implementation of finally in C++

Is this a good way to implement a Finally-like behavior in standard C++? (Without special pointers) class Exception : public Exception { public: virtual bool isException() { return true; } }; class NoException : public Exception { public:…
Tamara Wijsman
  • 12,198
  • 8
  • 53
  • 82
17
votes
5 answers

If I type Ctrl-C on the command line, will the finally block in Java still execute?

I'm running my Java application in cmd.exe in Windows. If I stop the process forcefully by pressing Ctrl-C, and the code at that moment was running in the try block, will the finally block still be executed? In my tests it seems that, yes, it is…
Hesey
  • 4,957
  • 6
  • 31
  • 31
16
votes
1 answer

Can an interrupted Java thread really skip a finally clause?

I was checking into the oft repeated rumour that daemon threads on the JVM treat finally blocks in some special way (they don't, ok?), when I read this, from the Oracle Java tutorial: Note: If the JVM exits while the try or catch code is being…
SusanW
  • 1,550
  • 1
  • 12
  • 22
16
votes
5 answers

Python: Is it possible to access the return value inside the `finally` clause?

I have a return statement inside a try clause: def f(): try: return whatever() finally: pass # How do I get what `whatever()` returned in here? Is it possible to get the return value inside the finally clause? This is more…
Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
16
votes
7 answers

Strange finally behaviour?

public class Test2 { public static void main(String[] args) { Test2 obj=new Test2(); String a=obj.go(); System.out.print(a); } public String go() { String q="hii"; try { return q; …
Harinder
  • 11,776
  • 16
  • 70
  • 126
14
votes
3 answers

Return in try & catch versus return in finally?

Is either one of these risky? Is one better? Or is it one of those things you print out and throw a dart at to decide? I want to do this now that I understand how finally works: try { stuff that changes something... } catch (System.Exception…
ChuckNeuros
  • 151
  • 1
  • 1
  • 5
13
votes
15 answers

Is there a favored idiom for mimicing Java's try/finally in C++?

Been doing Java for number of years so haven't been tracking C++. Has finally clause been added to C++ exception handling in the language definition? Is there a favored idiom that mimics Java's try/finally? Am also bothered that C++ doesn't have an…
RogerV
  • 3,826
  • 4
  • 28
  • 32
13
votes
3 answers

Will Python execute finally block after receiving Ctrl+C

If you stop a python script with Ctrl+C, will it execute any finally blocks, or will it literally stop the script where it is?
13
votes
4 answers

Is there such case when in try\finally block the finally won't be executed?

I'm studying for my test in Object Oriented Programming and I was wondering if there is any case what so ever that considering the following code: try { do something } catch (someException e) { } finally { do something } the finally block…
Dave
  • 141
  • 3
1 2
3
18 19