Python Exception and Errors

Python Exception and error


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.


Python Error

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.

var=100

if var>10

 print (“Variable – var=”, var)

 File “C:/Users/Documents/Python_raise.py”, line 2

 if var>10

 ^

SyntaxError: invalid syntax


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.

There is also possibility to raise built-in exception manually by using raise keyword. Visit this page to learn more about Python raise keyword.

ExceptionReason
AssertionErrorAssertionError raised when an assert statement fails
AttributeErrorAttributeError raised when an attribute reference or assignment fails
EOFErrorEOFError raised when the input() function reaches an EOF (end of file) condition without reading any data
FloatingPointErrorFloatingPointError raised when a floating point operation fails
GeneratorExitGeneratorExit raised when a generator‘s close() method is called. Generator – It is a function which returns an iterator.
ImportErrorImportError raised when an import statement fails to find the module definition or fails to find a name that is to be imported
IndexErrorIndexError raised when a sequence subscript is out of range
KeyErrorKeyError raised when a mapping (dictionary) key is not found in the set of existing keys
KeyboardInterruptKeyboardInterrupt raised when the user attempt to interrupt program execution with the interrupt key (normally Control-C or Delete).
MemoryErrorMemoryError raised when an operation runs out of memory
NameErrorNameError raised when interpreter didnt find a local or global variable name mentioned in the code.
NotImplementedErrorIn user defined base classes, abstract methods should raise this exception when they require derived classes to override the method
OSErrorOSError raised when a system function returns a system-related error e.g. disk full, file not found etc.
OverflowErrorOverflowError raised when the result of an arithmetic operation is too large to be represented
RuntimeErrorRuntimeError raised when an error is detected that doesn’t fall in any of the other categories
StopIterationStopIteration raised by functions next() and __next__() methods to signal that there are no further items produced by the iterator
SyntaxErrorSyntaxError raised when the parser encounters a syntax error
IndentationErrorBase class for syntax errors related to incorrect indentation
TabErrorTabError raised when indentation contains an inconsistent use of tabs and spaces
SystemErrorSystemError raised when the interpreter finds an internal error
SystemExitSystemExit raised by the sys.exit() function
TypeErrorTypeError raised when an operation or function is applied to an object of inappropriate type
UnicodeErrorUnicodeError raised when a Unicode-related encoding or decoding error occurs
ValueErrorValueError raised when a built-in operation or function receives an argument that has the right type but an inappropriate value
ZeroDivisionErrorZeroDivisionError raised when the second argument of a division or modulo operation is zero

Spread the love

You may also like

1 comment

Leave a Reply

Your email address will not be published. Required fields are marked *