python error & built-in exceptions
There are many built-in python exception an interpreter raises whenever the extraordinary situation encounters in the program.
People often gets confused with error and exception as both looks very similar but these two operates in a very different way.
Error encounters in the program when something wrong with the code. In error situation, program execution gets terminated and there is no way to handle this case unless you don’t fix the error.
Most common error observed with programmer is – syntax error. Observe this example to understand how error looks like in python.
This example demonstrates syntax error with if statement. You can see, semicolon is missing in if statement.
Python built-in Exceptions
The built-in exceptions are raised by interpreter or built-in functions whenever an extraordinary error situation occur in the code.
When an exception encounter in your code, program execution will get terminated unless you don’t handle specific situation in your code. HOW to handle an exception? Visit this page – Python try/except and finally Statements.
|AssertionError||AssertionError raised when an assert statement fails|
|AttributeError||AttributeError raised when an attribute reference or assignment fails|
|EOFError||EOFError raised when the input() function reaches an EOF (end of file) condition without reading any data|
|FloatingPointError||FloatingPointError raised when a floating point operation fails|
|GeneratorExit||GeneratorExit raised when a generator‘s close() method is called. Generator – It is a function which returns an iterator.|
|ImportError||ImportError raised when an import statement fails to find the module definition or fails to find a name that is to be imported|
|IndexError||IndexError raised when a sequence subscript is out of range|
|KeyError||KeyError raised when a mapping (dictionary) key is not found in the set of existing keys|
|KeyboardInterrupt||KeyboardInterrupt raised when the user attempt to interrupt program execution with the interrupt key (normally Control-C or Delete).|
|MemoryError||MemoryError raised when an operation runs out of memory|
|NameError||NameError raised when interpreter didnt find a local or global variable name mentioned in the code.|
|NotImplementedError||In user defined base classes, abstract methods should raise this exception when they require derived classes to override the method|
|OSError||OSError raised when a system function returns a system-related error e.g. disk full, file not found etc.|
|OverflowError||OverflowError raised when the result of an arithmetic operation is too large to be represented|
|RuntimeError||RuntimeError raised when an error is detected that doesn’t fall in any of the other categories|
|StopIteration||StopIteration raised by functions next() and __next__() methods to signal that there are no further items produced by the iterator|
|SyntaxError||SyntaxError raised when the parser encounters a syntax error|
|IndentationError||Base class for syntax errors related to incorrect indentation|
|TabError||TabError raised when indentation contains an inconsistent use of tabs and spaces|
|SystemError||SystemError raised when the interpreter finds an internal error|
|SystemExit||SystemExit raised by the sys.exit() function|
|TypeError||TypeError raised when an operation or function is applied to an object of inappropriate type|
|UnicodeError||UnicodeError raised when a Unicode-related encoding or decoding error occurs|
|ValueError||ValueError raised when a built-in operation or function receives an argument that has the right type but an inappropriate value|
|ZeroDivisionError||ZeroDivisionError raised when the second argument of a division or modulo operation is zero|