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...
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.
...
What exactly does a finally block in exception handling perform?
...
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
}
...
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...
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...
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...
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{
...
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...
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...
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 ...
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...
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...
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.
...
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...
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...
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.
...
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...
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...