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
|Sr. No||Name of Exception||Description|
|1||ArithmeticError||It is base class for all the errors that occur from numerical calculations.|
|2||AssertionError||This exception is raised when assert statement fails|
|3||AttributeError||This is raised when attribute assignment or referencing fails|
|4||EnvironmentError||It is raised for all errors which occurs outside python environment|
|5||EOFError||This Exception is raised when there is no input from the input() or raw_input()|
|6||Exception||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.
|7||FloatingPointError||It is raised when errors occurs from floating point calculations.|
|8||ImportError||This exceptions occurs when import statement fails.|
|9||IndexError||This exception is raised when index is not found in a sequence|
|10||IOErrors||It is raised in two cases:
1.If Input output operation fails
2.operating system related errors
|11||KeyboardInterrupt||This exception is raised when user breaks the flow of program by pressing Ctrl+c|
|12||KeyError||This exception is raised when keyis not found in a dictionary|
|13||LookupError||It is a base class for all lookup errors|
|14||NameError||This exception is raised when identifier is not found in a local or global namespace|
|15||NotImplementedError||This is raised if an abstract method that needs to be implemented in a inherited class is not actually implemented|
|16||OverflowError||This is raised when calculation exceeds maximum limit for numeric type|
|17||RuntimeError||This error is raised when error does not fall in any other category.|
|18||StandardError||It is base class for all the iterations except StopIteration and SystemExit|
|19||StopIteration||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.|
|20||SyntaxError||This is raised when there is error in Python syntax|
|21||SystemError||It is raised when Python interpreter detects the internal problem.|
|22||SystemExit||This is raised by sys.exit() function.If not handled in the code, causes interpreter to exit|
|23||TypeError||Raised when the built-in function is attempted which is not valid for a specified data type|
|24||UnboundLocalError||This exception is raised when trying to access a local variable in a function but no value has been assigned to it|
|25||ValueError||It is raised when function has valid number of arguments but they have invalid values.|
|26||ZeroDivisionError||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.