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

try-except-finally code not working as expected in threaded application

Execution abruptly halting if the thread / process is killed makes sense Why it won't execute cleanup code when I exit the main program normally by clicking the [X] on my terminal window? I'm still learning the ins-and-outs of multithreaded…
Matt Merrifield
  • 417
  • 6
  • 14
3
votes
2 answers

Try-Finally Equivalent in Caché ObjectScript

I'm looking for the equivalent semantics to the popular Try-Finally exception cleanup pattern, e.g. Why use try … finally without a catch clause? The idea is that you have cleanup steps that need to happen regardless of whether the code succeeds or…
Chris Smith
  • 5,326
  • 29
  • 29
3
votes
1 answer

Why doesn't finally block execute after playframework renderbinary?

I was recently surprised to find that the finally block in this play framework controller action code only got called after an Exception, but never when the call actually succeeded. try { InputStream is = getInputStreamMethod(); …
Jeremy Goodell
  • 18,225
  • 5
  • 35
  • 52
3
votes
2 answers

Using block: object initialization compiled into try block

In my project I have an object whose constructor can throw. So the code I'm using all across is as follows: MyObject obj = null; try { obj = new MyObject(); obj.DoSomething(); } finally { if (obj != null) obj.Free(); } As…
Dethariel
  • 3,394
  • 4
  • 33
  • 47
3
votes
2 answers

C# Web Service - Return then Finally - What happens first

In C#.NET, let's take the following example [WebMethod] public int TakeAction() { try { //Call method A Return 1; } catch (Exception e) { //Call method B Return 0; } finally { //Call method C …
adam
  • 2,930
  • 7
  • 54
  • 89
3
votes
5 answers

how the Try catch finally block is executed by JVM

According to Java Language Specification, Section §14.20.2 A try statement with a finally block is executed by first executing the try block. Then there is a choice: If execution of the try block completes normally, then the finally block…
Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58
3
votes
1 answer

How to properly dispose graphics context - do I need try and finally? (Java 1.7)

How to properly dispose graphics context - do I need to use try and finally? Simple example: public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g.create(); try { g2D.drawLine(0, 0, 10, 0); } finally { …
Ernestas Gruodis
  • 8,567
  • 14
  • 55
  • 117
3
votes
1 answer

Python Exception in finally clause eats prior exceptions

In my real case a Segmentation fault arises in the finally clause which I can't do anything about because it stems from an external library used via ctypes. Actually, I don't care about this segfault because the script is done anyway. However, the…
mknaf
  • 862
  • 8
  • 14
3
votes
2 answers

Double exception throwing in a try / finally block

Here's the code example : Try Throw New FirstException() Finally Throw New SecondException() End Try I figured out it only throws SecondException out and FirstException just vanishes. I thought FirstException would be inside InnerException…
Florian F.
  • 4,700
  • 26
  • 50
3
votes
1 answer

When should I use "try" blocks, and which kind should I use?

Two very basic questions about exception handling in Delphi. 1) When to Try? My guess is that I don't need a Try clause around strightforward code such as assignments, conditionals and loops access to my VCL compnents but that I do need to…
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
2
votes
4 answers

Is considered a bad practice or exist any drawback using try/finally try/except instead of begin/end?

In many places in some Apps which I maintain , I've found code which uses a try/finally or try/except block in a for loop or if sentence avoiding the use of begin/end Consider the next code (not production code, just a sample) {$APPTYPE…
Salvador
  • 16,132
  • 33
  • 143
  • 245
2
votes
2 answers

Do Ada 83 exceptions include resource cleanup?

Ada 83 was one of the first languages to have exceptions. (I want to say 'the first', but one thing I have learned from looking into the history of technology is that there is almost always an earlier X.) From the implementation perspective, the…
rwallace
  • 31,405
  • 40
  • 123
  • 242
2
votes
2 answers

Difference between code in JavaScript finally block and after try ... catch

This question may appear similar to others that have been answered, but I can't find an answer relating to JavaScript. What is the difference between code in a finally block and code that comes after the entire try ... catch ... finally…
Ben Zelnick
  • 433
  • 4
  • 14
2
votes
1 answer

What could be practical examples of using finally in the try block in Python

When does it make sense to use finally in the try..except block? Isn't listing statements just after the try..except doing the same? What's the difference between these two? try: result = 100 / 0 except ZeroDivisionError: print("division by…
Aidas Bendoraitis
  • 3,965
  • 1
  • 30
  • 45
2
votes
0 answers

Return statement in `finally` overrides return statement in `try`

I've run into some problems in my code, and so I have narrowed it down to this simple use-case: function func() { try { return true; } finally { return false; } } console.log(func()); To my extreme surprise, the…
goodvibration
  • 5,980
  • 4
  • 28
  • 61