Computer Science, asked by paliwal3419, 1 year ago

How to rethrow Python exception with new type?

Answers

Answered by nishitadeka82
6

HOLA BUDDY....

\huge\pink{Answer:-----}

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.....


gowthamcrontecpausiq: to contact
gowthamcrontecpausiq: as its difficult here
gowthamcrontecpausiq: to post big lines
gowthamcrontecpausiq: and
gowthamcrontecpausiq: i have many doubts
gowthamcrontecpausiq: woah!
gowthamcrontecpausiq: kk bye then
Answered by Anonymous
1

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

Similar questions