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

Using try-finally to execute statements after return

Consider the following code: Foo result = array[index]; index = (index + 1) % array.length; return result; In order to perform some final actions an extra variable is required. Does it make sense to write it as: try { return array[index]; }…
OLEGSHA
  • 388
  • 3
  • 13
2
votes
0 answers

Scanner Resource Leak

Today I have written a Java code that reads input from 2 txt files named input1.txt and input2.txt and solves 2 programming problems. It runs from the terminal like: java berkSol input1.txt input2.txt I am reading my inputs with Scanner in the…
BUY
  • 705
  • 1
  • 11
  • 30
2
votes
3 answers

Restoring saved values in a finally block?

I've seen this pattern used in a few different places now, but I'm not sure exactly what it's for or why it's needed. Given that I have seen it in quality projects, I'm sure it's useful, but I'd like to understand it rather than just blindly…
Steven Benitez
  • 10,936
  • 3
  • 39
  • 50
2
votes
3 answers

Use of nested "try/finally" "try/except" statements

I have seen this code posted here on StackOverflow: with TDownloadURL.Create(nil) do try URL := 'myurltodownload.com'; filename := 'locationtosaveto'; try ExecuteTarget(nil); except result := false; end; if not…
Gabriel
  • 20,797
  • 27
  • 159
  • 293
2
votes
2 answers

Try in finally block

try { operation1(); operation2(); ... } finally { try { finalizer_operation1(); finalizer_operation2(); } finally { very_critical_finalizer_operation_which_should_occurs_at_the_end(); …
Sinatr
  • 20,892
  • 15
  • 90
  • 319
2
votes
1 answer

How can I safely use semaphores in a while loop?

In the following code fragment I'm using a semaphore to synchronize access to certain resources. public void m () { permit.acquire (); while (!canFoo ()) { permit.release (); reticulateSpines (); permit.acquire (); …
mafu
  • 31,798
  • 42
  • 154
  • 247
2
votes
2 answers

C++ how to close a file securely without try {} finally{}?

Suppose a class like this: class A { private: QFile file; public: A::A(QFile file): file(file) {} void doSomething() { file.open(QIODevice::WriteOnly); // ... do operations that can throw an exception file.close(); }…
mabg
  • 1,894
  • 21
  • 28
2
votes
1 answer

Is it safe to dispose a JDialog in a try block, and then continue executing code in a matching finally block?

I have tried searching around for this question, as I imagine it must have been asked at some point, but this was the closest thing I could find Remove Top-Level Container on Runtime. My question is, is it safe execute code in a JDialog, after…
Ryan Latham
  • 59
  • 1
  • 8
2
votes
2 answers

C# Monitor behavior in case user quits application

I am using the following code for a critical section of a web page if(Monitor.TryEnter(lockObj,60000)) { try{ //write some things to a file } finally { Monitor.Exit(lockObj); } } Here, lockObj is a static member of the…
ankit0311
  • 735
  • 3
  • 10
  • 20
2
votes
2 answers

What's the scope of using the 'finally' clause in python?

Possible Duplicate: Purpose of else and finally in exception handling I'd like to understand why the finally clause exists in the try/except statement. I understand what it does, but clearly I'm missing something if it deserves a place in the…
chuse
  • 373
  • 3
  • 18
2
votes
3 answers

Testing the Return Value Within the Finally Block

All, this is a simple one. Is there a way of testing the returned value within a finally block without doing bool result = false; try { if (someCondition) { result = true; return result; } return result; } finally { …
MoonKnight
  • 23,214
  • 40
  • 145
  • 277
2
votes
3 answers

Behavior of a synchronized method with try and finally

Assume the following method: public synchronized void a(){ try{ System.out.println("a"); return; }finally{ System.out.println("a, finally"); } } I understand that the finally block will still be executed even…
Muhammad Gelbana
  • 3,890
  • 3
  • 43
  • 81
1
vote
2 answers

finally block not executing after Application.Run(new main_form)

I wanted to have some code execute before my program exited, so I thought that editing to the VS created Program.cs to look like: ... try { Application.Run(new main_form); } finally { special_shutdown_code(); } ... would do the trick, but…
nategoose
  • 12,054
  • 27
  • 42
1
vote
2 answers

What does "finally: pass" do in Python? (e.g. in try – except – finally)

I would have assumed a finally clause with only a pass to be pointless. But in a Bottle template, the following code for an optional include would not work without it. The result would contain everything before, and the included code itself, but…
Watchduck
  • 1,076
  • 1
  • 9
  • 29
1
vote
1 answer

Maintaining a roll-backable flow of code in python without extreme identation

I've encountered a situation where I'm working over a piece of code where I command changes on a remote object (that is one I can't duplicate to work over a clone), then ask the remote object for some operation in the new state and revert all the…
immortal
  • 3,118
  • 20
  • 38