If you want to add an extra check not provided by argparse
, such as:
if variable a == b then c should be not None
...is it permissible to raise ArgumentError
yourself?
Or, should you raise Exception
instead?
Also what is common practice for this kind of situation? Say that you add a piece of code that's almost like a local extension of the library. Should you use the same exception type(s) as those provided by the library you are extending?
First, we are raising an exception, here we can raise it in case it finds any situation where code stops execution if it continues the execution. So simply it will raise an issue and stop further execution.
ArgumentError is a descendant class of the StandardError superclass, and is typically raised when arguments passed to a method are incorrect, unexpected, or invalid in some way.
Raising exceptions When an error occurs in your program, you may either print a message and use sys. exit(1) to abort the program, or you may raise an exception. The latter task is easy. You just write raise E(message) , where E can be a known exception type in Python and message is a string explaining what is wrong.
There's nothing inherently wrong with raising an ArgumentError. You can use it anytime the arguments you receive are not what you expected them to be, including checking range of numbers.
Also, yes, in general it's alright for you to use the same exceptions provided by a given library if you are writing an extension to that library.
Regarding raising Exception
s, I wouldn't do that. You should always raise a specific exception so you know how to handle it in the code. Catching Exception
objects should be done at the highest level in your application, to catch and log all exceptions that you missed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With