Python Exceptions Standard Error
Contents |
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
Python Exceptions List
try statement with an except clause that mentions a particular python custom exception class, that clause also handles any exception classes derived from that class (but not exception classes from which it
Python Exception Message
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 python raise valueerror 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 and a string explaining the code). The associated value is the second argument to the raise statement. If the exception class is derived from python filenotfounderror 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-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. Ne
you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions. 8.1. Syntax Errors¶ Syntax errors, also known as parsing errors, are perhaps the most common kind
Python Exception Class Methods
of complaint you get while you are still learning Python: >>> while True print 'Hello python errno world' File "
Python Attributeerror Object Has No Attribute
displays a little ‘arrow' pointing at the earliest point in the line where the error was detected. The error is caused by (or at least detected at) the token preceding the arrow: in the example, the https://docs.python.org/2/library/exceptions.html error is detected at the keyword print, since a colon (':') is missing before it. File name and line number are printed so you know where to look in case the input came from a script. 8.2. Exceptions¶ Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are https://docs.python.org/2.7/tutorial/errors.html not unconditionally fatal: you will soon learn how to handle them in Python programs. Most exceptions are not handled by programs, however, and result in error messages as shown here: >>> 10 * (1/0) Traceback (most recent call last): File "
Exception StandardError ArithmeticError LookupError EnvironmentError Raised Exceptions AssertionError AttributeError EOFError FloatingPointError GeneratorExit IOError ImportError IndexError KeyError KeyboardInterrupt MemoryError NameError NotImplementedError OSError https://pymotw.com/2/exceptions/ OverflowError ReferenceError RuntimeError StopIteration SyntaxError SystemError SystemExit TypeError UnboundLocalError UnicodeError ValueError http://stackoverflow.com/questions/4096087/wheres-the-standard-python-exception-list-for-programmes-to-raise ZeroDivisionError Warning Categories Navigation Table of Contents Previous: Built-in Objects Next: String Services This Page Show Source Examples The output from all the example programs from PyMOTW has been generated with Python 2.7.8, unless otherwise noted. Some of the features described here may not be available in python exception earlier versions of Python. If you are looking for examples that work under Python 3, please refer to the PyMOTW-3 section of the site. Navigation index modules | next | previous | PyMOTW » Built-in Objects » exceptions - Built-in error classes¶ Purpose:The exceptions module defines the built-in errors used throughout the standard library and by the interpreter. Available python exceptions standard In:1.5 and later Description¶ In the past, Python has supported simple string messages as exceptions as well as classes. Since 1.5, all of the standard library modules use classes for exceptions. Starting with Python 2.5, string exceptions result in a DeprecationWarning, and support for string exceptions will be removed in the future. Base Classes¶ The exception classes are defined in a hierarchy, described in the standard library documentation. In addition to the obvious organizational benefits, exception inheritance is useful because related exceptions can be caught by catching their base class. In most cases, these base classes are not intended to be raised directly. BaseException¶ Base class for all exceptions. Implements logic for creating a string representation of the exception using str() from the arguments passed to the constructor. Exception¶ Base class for exceptions that do not result in quitting the running application. All user-defined exceptions should use Exception as a base class. StandardError¶ Base class for built-in exceptions used in the standard library. ArithmeticError¶ Base class for math-related errors. LookupError¶ Base class for erro
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 Where's the standard python exception list for programmes to raise? up vote 12 down vote favorite 3 There is a list standard python exceptions that we should watch out, but I don't think these are the ones we should raise ourselves, cause they are rarely applicable. I'm curious if there exists a list within standard python library, with exceptions similar to .NET's ApplicationException, ArgumentNullException, ArgumentOutOfRangeException, InvalidOperationException — exceptions that we can raise ourselves? Or is there different, more pythonic way to handle common error cases, than raising standard exceptions? EDIT: I'm not asking on how to handle exceptions but what types I can and should raise where needed. python exception error-handling share|improve this question edited Apr 22 '12 at 12:30 asked Nov 4 '10 at 11:18 Janusz Skonieczny 3,79862544 add a comment| 4 Answers 4 active oldest votes up vote 9 down vote accepted If the error matches the description of one of the standard python exception classes, then by all means throw it. Common ones to use are TypeError and ValueError, the list you linked to already is the standard list. If you want to have application specific ones, then subclassing Exception or one of it's descendants is the way to go. To reference the examples you gave from .NET ApplicationException is closest to RuntimeError ArgumentNullException will probably be an AttributeError (try and call the method you want, let python raise the exception a la duck typing) AttributeOutOfRange is just a more specific ValueError InvalidOperationException could be any number of roughly equivalent exceptions form the python standard lib. Basically, pick one that reflects whatever error it is yo