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

Powershell Finally block skipped with Ctrl-C

I'm writing a monitoring script in Powershell using a Try/Finally to log a message should the script end. The script is intended to run indefinitely, so I want a way to track unintended exiting. Every other StackOverflow post and Help page I've…
b_c
  • 1,202
  • 13
  • 24
7
votes
1 answer

How to handle throw exceptions inside finally block in java

In java, it is not recommended to throw exceptions inside finally section in try-chatch block due to hide the propagation of any unhandled throwable which was thrown in the try or catch block. This practice is a blocker level violation according to…
Hiran Perera
  • 736
  • 1
  • 5
  • 18
7
votes
8 answers

Should I put a try-finally block after every Object.Create?

I have a general question about best practice in OO Delphi. Currently, I put try-finally blocks anywhere I create an object to free that object after usage (to avoid memory leaks). E.g.: aObject := TObject.Create; try aOBject.AProcedure(); …
markus_ja
  • 2,931
  • 2
  • 28
  • 36
7
votes
5 answers

Is it safe to nest try/finally clauses like this?

As this is meant as a somewhat academic question regarding the behaviour of the try/finally clause, I tried to use an example that is very generic. Is there any danger in nesting a try/finally clause like this? openDatabaseConnection(); try { //…
Stephan B
  • 837
  • 1
  • 16
  • 41
7
votes
1 answer

JVM Synchronized Finally Blocks

Looking at the Java Virtual Machine Specification and compiled code tells us how "synchronized" blocks are implemented in java. The following code: public void testSync() { Object obj = getSomeObject(); synchronized (obj) { doSomething();…
leviathanbadger
  • 1,682
  • 15
  • 23
6
votes
2 answers

Does finally ensure some code gets run atomically, no matter what?

Assume I'm going to write a Python script that catches the KeyboardInterrupt exception to be able to get terminated by the user using Ctrl+C safely However, I can't put all critical actions (like file writes) into the catch block because it relies…
6
votes
2 answers

Finally block may not be called when enumerating over yielding method

I found a situation when finally block is not called. To the point: using System; using System.Collections.Generic; using System.Threading; using System.ComponentModel; class MainClass{ static IEnumerable SomeYieldingMethod(){ …
PanDenat
  • 61
  • 2
6
votes
5 answers

Why isn't the finally getting executed?

It was my assumption that the finally block always gets executed as long as the program is running. However, in this console app, the finally block does not seem to get executed. using System; using System.Collections.Generic; using…
Anish
  • 3,045
  • 3
  • 27
  • 29
5
votes
3 answers

On using "using" and "finally" to cleanup resources

Is there any case in which the following structure is needed? using (Something something = new Something()) { try { } finally { something.SomeCleanup(); } } Or, should all cleanup tasks be performed in the implicit…
isekaijin
  • 19,076
  • 18
  • 85
  • 153
5
votes
3 answers

Closing a cx_Oracle Connection While Allowing for a Down Database

The following cx_Oracle code works fine when the database is up: #!C:\Python27 import cx_Oracle try: conn = cx_Oracle.connect("scott/tiger@oracle") try: curs = conn.cursor() curs.execute("SELECT dummy FROM sys.dual") …
5
votes
1 answer

What is wrong with this Java Puzzlers piece of code?

In new, third edition of Effective Java Joshua Bloch mentions piece of code from Java Puzzlers (it's about closing resources in try-finally): For starters, I got it wrong on page 88 of Java Puzzlers, and no one noticed for years. In fact,…
ctomek
  • 1,696
  • 16
  • 35
5
votes
3 answers

Can a finally block know if there was an exception

In a Python program I have code with the following structure: try: value = my_function(*args) finally: with some_context_manager: do_something() if 'value' in locals(): do_something_else(value) But the 'value' in…
kasperd
  • 1,952
  • 1
  • 20
  • 31
5
votes
3 answers

How C# Using Statement Translates to Try-Finally

I'm trying to wrap my head around this. According to this page on Using statements: The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by…
TrueEddie
  • 2,183
  • 2
  • 26
  • 36
5
votes
6 answers

Understanding the 'finally' block

I've written seven test cases for understanding the behavior of the finally block. What is the logic behind how finally works? package core; public class Test { public static void main(String[] args) { new Test().testFinally(); } …
jai
  • 21,519
  • 31
  • 89
  • 120
5
votes
5 answers

try-finally block clarification

When I try to execute the following function in Java: public static int myfunc (int x) { try { return x; } finally { x++; } } public static void main (String args[]) { int y=5,z; z = myfunc(y); …
new_c_user
  • 123
  • 2
  • 12
1 2
3
12 13