Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rethrow an exception that contains information about an original exception?

So I basically have to isolate 2 layers of the application from one another by exceptions.

I have this WLST 12c script (python 2.2), that goes like

try:
    something something...
except java.lang.UnsuportedOpperationException, (a, b):
    pass
except java.lang.reflect.UndeclaredThrowableException, (a, b):
    pass

I'd like to be able to re-raise one of my own types of exception, that contains a message about what caused the previous exception (and no, i don't know what the a and b parameters are, but i'm guessing one of them should be the exception description).

I'm a java guy myself, so i am looking forward to something like

try {
    something something...
} catch (Exception e) {
    throw new RuntimeException(e, "something horrible happened");
}
like image 496
vlad-ardelean Avatar asked May 11 '12 17:05

vlad-ardelean


2 Answers

Although this is an old post, there is a much more simple answer to the original question. To rethrow an exception after catching it, just use "raise" with no arguments. The original stack trace will be preserved.

like image 83
normaldotcom Avatar answered Oct 14 '22 05:10

normaldotcom


I hope I got the question right.

I'm not sure about Python 2.2 specifics, but this says you can handle exceptions the same way it's done in more recent versions:

try:
    do_stuff()
except ErrorToCatch, e:
    raise ExceptionToThrow(e)

Or maybe the last line should be raise ExceptionToThrow(str(e)). That depends on how your exception is defined. Example:

try:
    raise TypeError('foo')
except TypeError, t:
    raise ValueError(t)

This raises ValueError('foo').

Hope it helps :)

like image 26
Lev Levitsky Avatar answered Oct 14 '22 05:10

Lev Levitsky