Python Argument Error Exception
Contents |
a try statement with an except clause that mentions a
Python Exception Message
particular class, that clause also handles any exception classes derived python custom exception from that class (but not exception classes from which it is derived). Two exception classes
Python Raise Valueerror
that are not related via subclassing are never equivalent, even if they have the same name. The built-in exceptions listed below can be generated by python filenotfounderror 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 of several items of information (e.g., an error code and a string explaining the code). The associated value is usually passed python exception class methods as arguments to the exception class's constructor. 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. When raising (or re-raising) an exception in an except or finally clause __context__ is automatically set to the last exception caught; if the new exception is not handled the traceback that is eventually displayed will include the originating exception(s) and the fina
you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions. 8.1. Syntax Errors¶
Python 3 Exceptions
Syntax errors, also known as parsing errors, are perhaps the most python exception stack trace common kind of complaint you get while you are still learning Python: >>> while True print 'Hello
Python Errno
world' File "
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 http://stackoverflow.com/questions/13422601/python-required-kwarg-which-exception-to-raise 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 http://stackoverflow.com/questions/8293325/is-it-bad-form-to-raise-argumenterror-by-hand 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 Python: python exception required kwarg, which exception to raise? up vote 20 down vote favorite One way to ensure that a method is called with a particular kwarg would be like: def mymethod(self, *args, **kwargs): assert "required_field" in kwargs Raising an AssertionError doesn't seem like the most appropriate thing to do. Is there an agreed upon builtin exception to handle this with a nice error message? More python argument error info: there are 3rd-party subclassing issues where *args and **kwargs kinda' need to be passed so making "required_field" a positional argument is not really a good option. python exception share|improve this question edited Nov 16 '12 at 19:25 asked Nov 16 '12 at 18:49 Skylar Saveland 5,82324363 4 I think the standard thing is to not make it a 'default argument'. –mgilson Nov 16 '12 at 18:50 there are already positional arguments that I don't want to mess with. –Skylar Saveland Nov 16 '12 at 19:03 More generally, there are also cases were kwargs can may accept numerous keys with some being required only if specific conditions are met with respect to the presence or values of other key word arguments. –Josh Heitzman Nov 16 '12 at 19:08 add a comment| 5 Answers 5 active oldest votes up vote 21 down vote accepted >>> def foo(bar): pass ... >>> foo() Traceback (most recent call last): File "
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 Is it bad form to raise ArgumentError by 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 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 handl