17

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?

DavidRR
  • 18,291
  • 25
  • 109
  • 191
Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141

1 Answers1

23

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

Paul Manta
  • 30,618
  • 31
  • 128
  • 208