finally

Get thrown exception in finally block.

Is there a way, how to get currently thrown exception (if exists)? I would like reduce amount of code and apply some reuse for task looks like: Exception thrownException = null; try { // some code with 3rd party classes, which can throw unexpected exceptions } catch( Exception exc ) { thrownException = exc; LogException( ex...

Multiple returns: Which one sets the final return value?

Given this code: String test() { try { return "1"; } finally { return "2"; } } Do the language specifications define the return value of a call to test()? In other words: Is it always the same in every JVM? In the Sun JVM the return value is 2, but I want to be sure, that this is not VM-dependant. ...

finally in exception handling

What exactly does a finally block in exception handling perform? ...

Exception from within a finally block

Consider the following code where LockDevice() could possibly fail and throw an exception on ist own. What happens in C# if an exception is raised from within a finally block? UnlockDevice(); try { DoSomethingWithDevice(); } finally { LockDevice(); // can fail with an exception } ...

Java try finally variations

This question nags me for a while but I did not found complete answer to it yet (e.g. this one is for C# http://stackoverflow.com/questions/463029/initializing-disposable-resources-outside-or-inside-try-finally). Consider two following Java code fragments: Closeable in = new FileInputStream("data.txt"); try { doSomething(in); } fina...

finally and return

Possible Duplicate: In Java, does return trump finally? In the below example, class ex8 { public void show() { try { int a=10/0; return; } catch(ArithmeticException e) { System.out.println(e); return; } finally...

Set reference = null in finally block?

A colleague of mine sets reference to null in finally blocks. I think this is nonsense. public Something getSomething() { JDBCConnection jdbc=null; try { jdbc=JDBCManager.getConnection(JDBCTypes.MYSQL); ... } finally { JDBCManager.free(jdbc); jdbc=null; // <-- Useful or not? } } What...

What is the gist of finally block in Java?

I think on the following examples; but could not figure out what the importance of the finally block is. Can you tell me the difference of the executions of these two code samples? Also a real life example can be helpful. Sample 1: try{ // some code 1 }catch(Exception ex){ // print exception }finally{ ...

understanding the finally block

I've written seven test cases for understanding the behavior of finally block. Can you guys explain the logic behind how finally works?? package core; public class Test { public static void main(String[] args) { new Test().testFinally(); } public void testFinally() { System.out.println("One = " + tryOne()); System.out.println...

Determine if executing in finally block due to exception being thrown

Is it possible to determine if code is currently executing in the context of a finally handler as a result of an exception being thrown? I'm rather fond of using the IDisposable pattern to implement entry/exit scoping functionality, but one concern with this pattern is that you might not necessarily want the end-of-scope behavior to occ...

How should I initialize variables that will be used in a try/catch/finaly block?

If I am using a try/catch/finally block where and how should I initialize variables? For example say I'm trying to use a FileStream . I want to catch any exceptions thrown while creating or using the stream. Then regardless of whether there were any problems or not I want to ensure any stream created is closed. So I'd do something like ...

How to properly handle ThreadInterruptedException?

Hello, public void threadMethod() { try { // do something } catch (ThreadInterruptedException e) { Console.WriteLine("[Net]", role, "Thread interrupted."); n.CloseConnection(); } finally { if (isAutenticated == false) { n.CloseConnection(); } Dispatcher.Invoke(add...

Determine if code called from an exception handler (using statement)?

I want to do something mildly silly. In my Dispose() method for an object, I want to print a debug trace for the object, telling me all events which happened while it was alive. But as this takes time and money, I only want to do this if Dispose() is being called because an exception was thrown. So I would like to do if (exceptionIsC...

finally doesn't seem to execute in C# console application while using F5

int i=0; try{ int j = 10/i; } catch(IOException e){} finally{ Console.WriteLine("In finally"); Console.ReadLine(); } The finally block does not seem to execute when pressing F5 in VS2008. I am using this code in Console Application. ...

Should Marshal.FreeHGlobal be placed in a finally block to ensure resources are disposed?

I have the following block of code: IntPtr unmanagedPointer = Marshal.AllocHGlobal(buffer.Length); Marshal.Copy(buffer, 0, unmanagedPointer, buffer.Length); SomeCommandThatCanThrowAnException(); Marshal.FreeHGlobal(unmanagedPointer); Should the block be wrapped in a try, and the FreeHGlobal command be placed in a finally block. (In ca...

Using python "with" statement with try-except block

Is this the right way to use the python "with" statement in combination with a try-except block?: try: with open("file", "r") as f: line = f.readline() except IOError: <whatever> If it is, then considering the old way of doing things: try: f = open("file", "r") line = f.readline() except IOError: <whatever...

Finally: is it guaranteed to be invoked in any case

Is there any even small possibility that finally will not be invoked but application still be running? I'm releasing semaphore there finally { _semParallelUpdates.Release(); } and afraid of lost of some of them. ...

loss exception in block catch

I run this code: public class User { public static void main(String args[]) { int array[] = new int[10]; int i = 1; try { System.out.println("try: " + i++); System.out.println(array[10]); System.out.println("try"); } catch (Exception e) { System.out.pri...

Bash: Finally (Try, Except) Exception

I want to execute some commands on the end of the bash script, even if the user press CTRL+C to cancel its execution. I know I can run the bash script from inside another programming language (for example, Python), so that I can use the 'finally' (try-finally) block to execute some code. But knowing that StackOverflow is a center for e...