Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad form to raise ArgumentError by hand?

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?

like image 233
Roman A. Taycher Avatar asked Nov 28 '11 08:11

Roman A. Taycher


People also ask

Does raising an exception stop execution Ruby?

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.

What is argument error?

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.

How do you increase input error in Python?

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.


1 Answers

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.

like image 153
Paul Manta Avatar answered Sep 20 '22 14:09

Paul Manta