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

Workaround for python 2.4's yield not allowed in try block with finally clause

I'm stuck on python2.4, so I can't use a finally clause with generators or yield. Is there any way to work around this? I can't find any mentions of how to work around this limitation in python 2.4, and I'm not a big fan of the workarounds I've…
Richard Levasseur
  • 14,562
  • 6
  • 50
  • 63
4
votes
3 answers

Discover if exception is thrown in finally part of try-finally block

I have a simple question an simple part of code, some basic try-finally block: try { // Some code which can throw an Exception } finally { // Some code which also can throw an Exception } My main question would be: Is there any way we can…
partlov
  • 13,789
  • 6
  • 63
  • 82
4
votes
2 answers

try ,catch, finally execution

Possible Duplicate: throws Exception in finally blocks The catch block is only executed if an exception is thrown in the try block. The finally block is executed always after the try(-catch) block, if an exception is thrown or not. My question…
Hardik Nadiyapara
  • 2,436
  • 2
  • 17
  • 24
3
votes
2 answers

How to ensure (like a try-finally) destruction of a HEAP-ALLOCATED object

I'm looking for a way to ensure that an object that is executed on the heap is ALWAYS deallocated when I'm done with it. I know that if it's allocated on the stack, I can use RAII to ensure it will be taken care of - unfortunately, this won't work…
Paul Molodowitch
  • 1,366
  • 3
  • 12
  • 29
3
votes
1 answer

Is try-finally being deprecated with Java 18+?

JEP 421, being released in the forthcoming Java 18, deprecates finalization. I understand this mean the finalize() method is being deprecated. However, it also mentions the try/finally block and mentions try-with-resources as an alternative, so I'm…
k314159
  • 5,051
  • 10
  • 32
3
votes
1 answer

Why does returning a variable in a try block not change the value of the returned thing when this variable is reset in the finally section?

I don't really understand how the instructions flow in the following code. The body of finally is guaranteed to be executed before the method returns. If so, the return value should be 0 rather than 1. Could you explain the internal mechanism why…
Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165
3
votes
2 answers

database connection using "try finally "

Can someone enlighten me on handling the database connection (and errors) using try finally ? What would be the best practice ? Seen various styles but I wonder what would be the best approach. Should opening of the tables be put in TRY block or…
user763539
  • 3,509
  • 6
  • 44
  • 103
3
votes
3 answers

in C# try-finally how to catch the original exception

My simple example is: void FixedUnalterableMethod() { try { throw new Exception("Exception 1"); //line 12. } finally { throw new Exception("Exception 2"); //line 16. } …
Cyrus
  • 2,261
  • 2
  • 22
  • 37
3
votes
2 answers

java try finally unlock idiom

Javadoc and some answers(Threads - Why a Lock has to be followed by try and finally) state that: In most cases, the following idiom should be used: Lock l = ...; l.lock(); try { // access the resource protected by this lock } finally { …
osseum
  • 187
  • 14
3
votes
1 answer

try-finally with close auto-refactoring to try-with-resources with codestyle/checkstyle

I am working on a codebase that has recently migrated from Java 6 to Java 7. I would like to replace constructions like this: Connection conn = null; try{ conn = new Connection(); ... } catch(Exception ex){ ... } finally{ if (conn !=…
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
3
votes
0 answers

Interlocked.Increment in finally block

Looking through the sources of .NET Core, I've found the following piece in ConcurrentQueue source: //We need do Interlocked.Increment and value/state update in a finally block to ensure that they run //without interuption. This is to prevent…
DixonD
  • 6,557
  • 5
  • 31
  • 52
3
votes
0 answers

Why doesn't code trying to catch a StackOverflowError run?

This question extends from Try-finally block prevents StackOverflowError. If I add a catch block, why catch block codes never runs? public static void foo() { try { foo(); } catch(StackOverflowError e) { …
atealxt
  • 186
  • 1
  • 6
3
votes
3 answers

Dealing with in a finally block

I have some code which creates a JarFile and a URLClassLoader, both of which I want to close at the end. Naturally, I decided to use the finally block to deal with the cleanup: JarFile jar = ...; URLClassLoader loader = ...; try { // work ... }…
Martin Tuskevicius
  • 2,590
  • 4
  • 29
  • 46
3
votes
2 answers

Is it reasonable to wrap an entire main loop in a try..finally block?

I've made a map editor in Python2.7.9 for a small project and I'm looking for ways to preserve the data I edit in the event of some unhandled exception. My editor already has a method for saving out data, and my current solution is to have the main…
Augusta
  • 7,171
  • 5
  • 24
  • 39
3
votes
0 answers

Why does `finally: return 42` clear an uncaught exception?

Today this interesting piece of python code was posted on Twitter. def f(): try: raise KeyError finally: return 42 g = f() When executing it, f() returns 42 and there's no exception bubbling up the stack as I'd expect.…
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636