Python Argument Error
Contents |
This module never needs to be imported explicitly: the exceptions are provided in the built-in namespace as well as python exception message the exceptions module. For class exceptions, in a try
Python Custom Exception
statement with an except clause that mentions a particular class, that clause also handles any python raise valueerror exception classes derived from that class (but not exception classes from which it is derived). Two exception classes that are not related via subclassing are never python filenotfounderror equivalent, even if they have the same name. The built-in exceptions listed below can be generated by the interpreter or built-in functions. Except where mentioned, they have an "associated value" indicating the detailed cause of the error. This may be a string or a tuple containing several items of information (e.g., an error code
Python Exception Class Methods
and a string explaining the code). The associated value is the second argument to the raise statement. If the exception class is derived from the standard root class BaseException, the associated value is present as the exception instance's args attribute. User code can raise built-in exceptions. This can be used to test an exception handler or to report an error condition "just like" the situation in which the interpreter raises the same exception; but beware that there is nothing to prevent user code from raising an inappropriate error. The built-in exception classes can be subclassed to define new exceptions; programmers are encouraged to derive new exceptions from the Exception class or one of its subclasses, and not from BaseException. More information on defining exceptions is available in the Python Tutorial under User-defined Exceptions. The following exceptions are only used as base classes for other exceptions. exception BaseException¶ The base class for all built
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow python errno the company Business Learn more about hiring developers or posting ads with us Stack python attributeerror object has no attribute Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of
Python Exception Stack Trace
6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Which exception should I raise on bad/illegal argument combinations in Python? up vote 236 down vote favorite https://docs.python.org/2/library/exceptions.html 26 I was wondering about the best practices for indicating invalid argument combinations in Python. I've come across a few situations where you have a function like so: def import_to_orm(name, save=False, recurse=False): """ :param name: Name of some external entity to import. :param save: Save the ORM object before returning. :param recurse: Attempt to import associated objects as well. Because you need the original object to have a key to relate to, http://stackoverflow.com/questions/256222/which-exception-should-i-raise-on-bad-illegal-argument-combinations-in-python save must be `True` for recurse to be `True`. :raise BadValueError: If `recurse and not save`. :return: The ORM object. """ pass The only annoyance with this is that every package has its own, usually slightly differing BadValueError. I know that in Java there exists java.lang.IllegalArgumentException -- is it well understood that everybody will be creating their own BadValueErrors in Python or is there another, preferred method? python exception arguments share|improve this question edited Nov 2 '11 at 19:26 sth 128k33204309 asked Nov 1 '08 at 23:02 cdleary 27.4k40130178 add a comment| 4 Answers 4 active oldest votes up vote 245 down vote accepted I would just raise ValueError, unless you need a more specific exception.. def import_to_orm(name, save=False, recurse=False): if recurse and not save: raise ValueError("save must be True if recurse is True") There's really no point in doing class BadValueError(ValueError):pass - your custom class is identical in use to ValueError, so why not use that? share|improve this answer edited Apr 29 at 18:04 izrik 38629 answered Nov 1 '08 at 23:37 dbr 93.3k46225295 Agreed -- I almost always use ValueError for stuff like this, too. –mipadi Nov 1 '08 at 23:58 17 > "so why not use that?" - Specificity. Perhaps I want to catch at some outer layer
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us http://stackoverflow.com/questions/8293325/is-it-bad-form-to-raise-argumenterror-by-hand Learn more about Stack Overflow the company Business Learn more about hiring developers http://stackoverflow.com/questions/8107713/using-argparse-argumenterror-in-python or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Is it bad form to raise ArgumentError by python exception hand? up vote 12 down vote favorite 1 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 python argument error a local extension of the library. Should you use the same exception type(s) as those provided by the library you are extending? python exception-handling argparse share|improve this question edited Mar 12 '15 at 12:49 DavidRR 5,20472747 asked Nov 28 '11 at 8:40 Roman A. Taycher 3,834144796 4 For my own reference: the Python builtin is ValueError, not ArgumentError –SystemParadox May 1 '13 at 11:38 2 Related: How to use argparse.ArgumentError in Python –DavidRR Mar 12 '15 at 12:44 add a comment| 1 Answer 1 active oldest votes up vote 15 down vote accepted 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. share|impr
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Using 'argparse.ArgumentError' in Python up vote 21 down vote favorite 4 I'd like to use the ArgumentError exception in the argparse module in Python, but I can't figure out how to use it. The signature says that it should be called as ArgumentError(argument, message), but I can't figure out what argument should be. I think it should be some part of the parser object, but I couldn't find any documentation for it. python argparse share|improve this question edited Mar 12 '15 at 12:53 DavidRR 5,20472747 asked Nov 12 '11 at 21:34 asmeurer 37.2k1165131 Related: Is it bad form to raise ArgumentError by hand? –DavidRR Mar 12 '15 at 12:50 You can use it within a custom Action class, just as the existing classes do. But I can't imagine a reason to do so outside of the parser. It doesn't anything special - just adds the Action name to the error message. Study its use in the code. –hpaulj Mar 12 '15 at 17:08 add a comment| 2 Answers 2 active oldest votes up vote 28 down vote accepted From the source documentation: ArgumentError: The exception raised by ArgumentParser objects when there are errors with the parser's actions. Errors raised while parsing the command-line are caught by ArgumentParser and emitted as command-line messages. The argument parameter of the constructor is the Action object from which the exception is raised. Usually there is no need to raise it outside of an Action subclass, and when subclassing it, there is no need to raise it explicitly; you usually raise ValueError (or whatever is appropriate) instead. Float values between 0 and 1 In regards to your comment, that you only want to accept floating point values between 0 and 1. For this you should use the ability to define