How to rethrow Python exception with new type?
Answers
HOLA BUDDY....
It looks like you want to chain your new exception off of the existing one. That can be done, using syntax introduced with PEP 3134. Alternatively, you could completely suppress the previous exception, using syntax added in PEP 409.
To chain the exceptions, give the caught exception a name with as in the except statement, then use the name with from at the end of your raise statement. Something like this:
except sqlite3.Error as e:
raise FileParseError("Not a valid database: '%s'", fname) from e
If you want to completely suppress the sqlite error, rather than just translating it to another exception type, you can use from None in the raise statement:
except sqlite3.Error:
raise FileParseError("Not a valid database: '%s'", fname) from None
The internal exception is still available when the context has been suppressed, but it will not be printed out in the traceback.
#hope it helps you.....
Hello Mate,
It looks like you want to chain your new exception off of the existing one. That can be done, using syntax introduced with PEP 3134. Alternatively, you could completely suppress the previous exception, using syntax added in PEP 409.
Hope this helps you