Quantcast
Channel: Possible to raise two types of errors? - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Answer by gilch for Possible to raise two types of errors?

$
0
0

There's really no need to raise two exceptions simultaneously. It's fine to raise the first error you detect. Yes, a TypeError is appropriate if you expected a string but got something else. You can do the check and raise the TypeError either before or after the ValueError. You can catch multiple exception types in the same try statement by adding more except clauses.

Another option is to use an assert statement to validate all the arguments. An assertion indicates that a violation is a bug in the program, and you don't want to recover by using a try statement.

I should mention that it is possible to chain exceptions in Python (using the raise ... from ... syntax), but this used when translating from one exception type to another or for when one exception causes another, which does not seem to apply to this case.


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>