sacul
asked on
lost exception
I have a simple question.
If an exception is generated and a new exception is generated due to re-throwing the information about the first exception is lost. What will happen to the error in the try block if the original exception is lost. I am trying to make sense of it and it is a very vague part of exceptions so if anyone can aid me in this matter I would be very happy.
Thanks,
Zac
If an exception is generated and a new exception is generated due to re-throwing the information about the first exception is lost. What will happen to the error in the try block if the original exception is lost. I am trying to make sense of it and it is a very vague part of exceptions so if anyone can aid me in this matter I would be very happy.
Thanks,
Zac
> and a new exception is generated due to re-throwing the
> information about the first exception is lost.
If you r simply rethrowing the exception, then no new exception is created and the first exception is not lost.
> information about the first exception is lost.
If you r simply rethrowing the exception, then no new exception is created and the first exception is not lost.
ASKER
ok..well what if the exception is caught and re-thrown from the catch block. This result in a new exception(I think). For instance if you call the fillInStackTrace() and assign this to a variable that references an Exception. Then that Exception can be re-thrown. But if an exception is re-thrown from the catch block that is not related to the error, is this possible? and is the information encapsulated in the original object lost? will the program stop?
I was reading up on this and this is what the text said,
"Information about the exception can also be lost if while re-throwing, a new exception is created. The original exception is lost."
Thanks
Zac
I was reading up on this and this is what the text said,
"Information about the exception can also be lost if while re-throwing, a new exception is created. The original exception is lost."
Thanks
Zac
Depends what you mean by re-throwing.
catch (Exception ex)
{
throw ex; // same exception is thrown
}
catch (Exception ex)
{
throw new Exception("blah blah"); // new exception, original lost
}
catch (Exception ex)
{
throw ex; // same exception is thrown
}
catch (Exception ex)
{
throw new Exception("blah blah"); // new exception, original lost
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I dont get it. The exception variable is defined as part of the catch block, and it is no different than any other block:
{
int i = 25; // 'i' is local to this block just like 'ex' is local to object's catch-block abowe
throw new Exception( "bye" );
...
}
when the exception is thrown that block gets left behind and the variable i is lost.
{
int i = 25; // 'i' is local to this block just like 'ex' is local to object's catch-block abowe
throw new Exception( "bye" );
...
}
when the exception is thrown that block gets left behind and the variable i is lost.
not exactly, yes the variable goes out of scope. But the instance that it references is not lost.
eg.
public Object someFunction()
{
String s = "abc";
return s;
}
eg.
public Object someFunction()
{
String s = "abc";
return s;
}
True, you can send the data out of the block before it terminate. but isnt that still bogstandard Java scope rules.
> but isnt that still bogstandard Java scope rules.
Yes, exceptions are simply object instances like anything else.
Yes, exceptions are simply object instances like anything else.
ASKER
> catch (Exception ex)
{
throw new Exception("blah blah"); // new exception, original lost
}
So you would never throw a new Exception in the catch block then with the risk of loosing the original one? and if you do would the execution in main stop?
thanks,
Zac
{
throw new Exception("blah blah"); // new exception, original lost
}
So you would never throw a new Exception in the catch block then with the risk of loosing the original one? and if you do would the execution in main stop?
thanks,
Zac
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No, main would not stop. All that would happen when 'losing' the exception is that you would not have acces to the information it contained any more.
> then with the risk of loosing the original one?
There's not really any risk, you'd only do that if you did not need the original exception anymore.
There's not really any risk, you'd only do that if you did not need the original exception anymore.
ASKER
ok I got the theoretical part of it now I just have to understand why you want to throw a new exception in a catch block, but that is for another time. I haven't been programming java for long and this is a somewhat diffuse topic. Thanks to you, experts, I know have a better understanding of it.
thank you,
Zac
thank you,
Zac
I guess the point is that exceptions throw themselves when you try to do someting you cannot. So inside your catch-block you should be extra carefull, knowing that your program logic assumptions are already a bit dubious seeing as how the first exception got thrown.
regards JakobA
regards JakobA
regards JakobA