Python Invalid Argument Error
Contents |
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 what is the argument of an exception in python quiz company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions python valueerror example Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million
Python Custom Exception
programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How does one indicate invalid arguments in Python? [duplicate] up vote 8 down vote favorite Possible Duplicate: python: Should I
Python Exception Get Message
use ValueError or create my own subclass to handle invalid strings? Reading Built-in Exceptions I read: All user-defined exceptions should also be derived from this class" with regards to Exception. I also see a ValueError which says: Raised when a built-in operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as IndexError. If I want to python raise typeerror raise an exception for invalid arguments (the equivalent of Ruby's ArgumentError), what should I do? Should I raise ValueError directly, or preferably subclass ValueError with my own intention revealing name? In my case, I accept a key argument, but I want to restrict the set of characters in the key, such that only /\A[\w.]+\Z/ (Perl/Ruby regular expression) is accepted. python exception share|improve this question asked Feb 25 '11 at 2:39 Francois Beausoleil 8,95484264 marked as duplicate by Zooba, Francois Beausoleil, Jochen Ritzel, Chris Lutz, Josh Lee Feb 25 '11 at 2:58 This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. add a comment| 2 Answers 2 active oldest votes up vote 6 down vote accepted I think the general idea is this: ValueError should almost always denote some sort of client error (where 'client' means the programmer using your interface). There are two high-level types of exceptions in Python: uncommon cases for otherwise normally functioning code; the client is not to blame usage errors where some interface is used incorrectly, or the through a series of interface calls, the system has reached an inconsistent state; time to blame the client In my opinion, for the first case, it makes sense to crea
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
Invalid Argument Exception Java
Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs name of errors in python Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, error message indicates in python just like you, helping each other. Join them; it only takes a minute: Sign up Invalid argument raise exception up vote 0 down vote favorite How do I test my parameter if it will raise an exception http://stackoverflow.com/questions/5113014/how-does-one-indicate-invalid-arguments-in-python without actually raising it, using try and except? class MyClass: def function(parameter): pass parameter is an ambiguous function that may raise 1 or more of any exception, for example: parameter = pow("5", 5) A TypeError is raised as soon as the function is called and before the function can execute its statements. python class exception parameters share|improve this question edited Jan 26 '12 at 23:53 asked Jan 26 '12 at 22:52 Christopher Markieta 1,37852446 http://stackoverflow.com/questions/9026675/invalid-argument-raise-exception Why don't you want to use try ... except? Just for curiosity or is there any other reason? –Praveen Gollakota Jan 26 '12 at 23:00 Sorry if I was unclear, I do want to use try and except, but I don't know how it would work in the parameters. –Christopher Markieta Jan 26 '12 at 23:02 What your parameter will raise depends on how your function is implemented. In your example I don't see how parameter could raise an exception inside function. –Rik Poggi Jan 26 '12 at 23:06 If parameter is invalid it will raise an exception as soon as it is called. I want to be able to "catch" the exception with try and except. I do not have any information on what parameter will be except that it is a function and will raise any exception. –Christopher Markieta Jan 26 '12 at 23:10 Sorry, I can't make any sense of this. What do you mean by parameter being invalid? What do you mean by "it might/will raise an exception"? Values don't raise exceptions, operations do. You have to try to do something with the parameter for any problem to occur. –Karl Knechtel Jan 26 '12 at 23:42 | show 2 more comments 4 Answers 4 active oldest votes up
This module never needs to be imported explicitly: the exceptions are provided in the built-in namespace as well as the exceptions module. For class exceptions, in a try https://docs.python.org/2/library/exceptions.html statement with an except clause that mentions a particular class, that clause also handles any 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 equivalent, even if they have the same name. The built-in exceptions listed below can be generated by the interpreter or built-in in python 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 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, python invalid argument 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-in exceptions. It is not meant to be directly inherited by user-defined classes (for that, use Exception). If str() or unicode() is called on an instance of this class, the representation of the argument(s) to the instance are returned, or the empty string when there were no arguments. New in version 2.5. args¶ The tuple of arguments given to the exc