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
5
votes
5 answers

Stack overflow error handling in finally block

I have a program in java, which runs infinite times. Program code: void asd() { try { //inside try block System.out.println("Inside try !!!"); asd(); } finally { //inside finally …
ideano1
  • 140
  • 1
  • 10
5
votes
1 answer

Memory leak in tornado generator engine with try/finally block when connections are closed

This awesome code, shows memory leak in tornado's gen module, when connections are closed without reading the response: import gc from tornado import web, ioloop, gen class MainHandler(web.RequestHandler): @web.asynchronous @gen.engine …
Taha Jahangir
  • 4,774
  • 2
  • 42
  • 49
5
votes
8 answers

In java, is there a way to ensure that multiple methods get called in a finally block?

So I have a try/finally block. I need to execute a number of methods in the finally block. However, each one of those methods can throw an exception. Is there a way to ensure that all these methods are called (or attempted) without nested finally…
sangfroid
  • 3,733
  • 11
  • 38
  • 42
5
votes
5 answers

finally not called after try

For some reason within my console application I cannot get my finally block to run. I was writing this code to test how the finally block works so it is very simple: static void Main() { int i = 0; try { int j = 1 / i; //…
Fr33dan
  • 4,227
  • 3
  • 35
  • 62
4
votes
2 answers

C# Console App Not Calling Finally Block

I'm writing a console app to run as a scheduled task and it doesn't appear to execute the finally block of the running code when you close it using the close button. I've tried to replicate this behaviour with the following very simple console…
Jackson Pope
  • 14,520
  • 6
  • 56
  • 80
4
votes
1 answer

Response.Redirect() inside a try-finally

Possible Duplicate: Will code in finally run after a redirect? Hello, What happens when I call a Response.Redirect() with EndResponse set to true/false inisde a try/finally block? Will the finally be called?
NLV
  • 21,141
  • 40
  • 118
  • 183
4
votes
3 answers

How to use try-finally construction in C#?

We've seen many questions about try-catch-finally and try-finally constructions on this forum. The number of answers increases the number of questions, so I have few too. Here's a link into Microsoft explanation try-finally construction. I've…
4
votes
0 answers

Why is a finally block *sometimes* not executed on ThreadAbortException if it contains an await?

UPDATE: I don't think this question is a duplicate of Can ThreadAbortException skip finally? because (1) I'm not creating another thread, so there's no possibility of a race condition, and (2) this behavior only occurs if the finally block contains…
Michael Liu
  • 52,147
  • 13
  • 117
  • 150
4
votes
1 answer

Is there any purpose for an empty Try/Finally block?

I have inherited a large code base that is full of constructs like this: try DoWhatever; finally end; Sometimes "DoWhatever" involves some fiddling with controls, and very often it's a post…
user3810626
  • 6,798
  • 3
  • 14
  • 19
4
votes
1 answer

Return from try block in try finally block

I have two code snippets in both I return from try and have finally block too.The first one works fine and prints from finally too and later gives compile time error at line marked line1. 1st snippet class abc { public static void main(String…
Vishal
  • 296
  • 3
  • 11
4
votes
2 answers

finally statement doesn't take effect in a thread

According to the official python documentation, "finally" statement will always be executed, and thus is usually used for clean-up operations. If "finally" is present, it specifies a ‘cleanup’ handler. The "try" clause is executed, including any…
4
votes
8 answers

throw-catch logic

try { try { throw new Exception("From Try"); } catch { throw new Exception("From Catch"); } finally { throw new Exception("From Finally"); } } catch (Exception ex) { …
DxCK
  • 4,402
  • 7
  • 50
  • 89
4
votes
2 answers

Can a Synchronized Block be simplified to a Try-Finally Block on the Bytecode Level?

Writing my own compiler for a Java-like language, I am having trouble compiling synchronized blocks. I come up with the following idea to simplify them to try-finally blocks: synchonized (obj) { statements... } Can be replaced with Object…
Clashsoft
  • 11,553
  • 5
  • 40
  • 79
4
votes
2 answers

getting asynchronous socket error 10049 even if i use try..except

when ever i run my program(outside the debugger/ide) i get error asynchronous socket error 10049, am i not supposed to recieve a message dialoge : ''error''? see my code below begin try ClientSocket1.open; except …
Omair Iqbal
  • 1,820
  • 1
  • 28
  • 41
4
votes
2 answers

Setting field value in try{} and calling base class in finally{}

I was browsing .NET Framework source code trying to understand another issue and I saw this code (in PeerNearMe.cs from System.Net.PeerToPeer.Collaboration): private bool m_Disposed; protected override void Dispose(bool disposing) { if…
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208