Table of Contents
Exceptions in Python
What is exception?
An event which may occur during execution of program ,that interrupts normal flow of program is called an exception.
Exception is an abnormal condition in program code.When error occurs ,Python generates exceptions which can be handled and avoids program from getting crash.
How to handle exceptions?
Whenever an event occurs which is unhandleable by python script, it raises an exception.
Whenever an exception occurs it needs to be handled immediately else it will terminates the script and quit.
If we think that there is possibility of error in our code we can use exception handling priorly before execution of code.
We can raise an exception in program by using the raise statement.
Errors are generally handled by saving state of execution at the instant when error occured or by interrupting normal flow of program to execute a particular function called as exception handler.
Exceptions can be of two major types: built-in exceptions & user defined exceptions.
Built-in Exceptions in Python
|Name of Exception
|It is base class for all the errors that occur from numerical calculations.
|This exception is raised when assert statement fails
|This is raised when attribute assignment or referencing fails
|It is raised for all errors which occurs outside python environment
|This Exception is raised when there is no input from the input() or raw_input()
|It is the base class for all exceptions.
All other exceptions are derived from this base class.
Even user defined exceptions needs to import this class to create their own exception.
|It is raised when errors occurs from floating point calculations.
|This exceptions occurs when import statement fails.
|This exception is raised when index is not found in a sequence
|It is raised in two cases:
1.If Input output operation fails
2.operating system related errors
|This exception is raised when user breaks the flow of program by pressing Ctrl+c
|This exception is raised when keyis not found in a dictionary
|It is a base class for all lookup errors
|This exception is raised when identifier is not found in a local or global namespace
|This is raised if an abstract method that needs to be implemented in a inherited class is not actually implemented
|This is raised when calculation exceeds maximum limit for numeric type
|This error is raised when error does not fall in any other category.
|It is base class for all the iterations except StopIteration and SystemExit
|This exception is raised when next() method of an iterator reaches at the end of an iterable and has no other object to point at.
|This is raised when there is error in Python syntax
|It is raised when Python interpreter detects the internal problem.
|This is raised by sys.exit() function.If not handled in the code, causes interpreter to exit
|Raised when the built-in function is attempted which is not valid for a specified data type
|This exception is raised when trying to access a local variable in a function but no value has been assigned to it
|It is raised when function has valid number of arguments but they have invalid values.
|It is raised when division or modulo by zero takes place for all numeric types.
Note :Conventionally Exception names should have their first letters capital.