Exceptions in Python

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. NoName of Exception Description
1ArithmeticErrorIt is base class for all the errors that occur from numerical calculations.
2AssertionErrorThis exception is raised when assert statement fails
3AttributeErrorThis is raised when attribute assignment or referencing fails
4EnvironmentError It is raised for all errors which occurs outside python environment
5EOFErrorThis Exception is raised when there is no input from the input() or raw_input()
6Exception 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.
7FloatingPointErrorIt is raised when errors occurs from floating point calculations.
8ImportErrorThis exceptions occurs when import statement fails.
9IndexErrorThis exception is raised when index is not found in a sequence
10IOErrorsIt is raised in two cases:
1.If Input output operation fails
2.operating system related errors
11KeyboardInterruptThis exception is raised when user breaks the flow of program by pressing Ctrl+c
12KeyErrorThis exception is raised when keyis not found in a dictionary
13LookupError It is a base class for all lookup errors
14NameErrorThis exception is raised when identifier is not found in a local or global namespace
15NotImplementedErrorThis is raised if an abstract method that needs to be implemented in a inherited class is not actually implemented
16OverflowErrorThis is raised when calculation exceeds maximum limit for numeric type
17RuntimeErrorThis error is raised when error does not fall in any other category.
18StandardErrorIt is base class for all the iterations except StopIteration and SystemExit
19StopIterationThis exception is raised when next() method of an iterator reaches at the end of an iterable and has no other object to point at.
20SyntaxErrorThis is raised when there is error in Python syntax
21SystemErrorIt is raised when Python interpreter detects the internal problem.
22SystemExitThis is raised by sys.exit() function.If not handled in the code, causes interpreter to exit
23TypeErrorRaised when the built-in function is attempted which is not valid for a specified data type
24UnboundLocalErrorThis exception is raised when trying to access a local variable in a function but no value has been assigned to it
25ValueErrorIt is raised when function has valid number of arguments but they have invalid values.
26ZeroDivisionErrorIt is raised when division or modulo by zero takes place for all numeric types.

Note :Conventionally Exception names should have their first letters capital.

Leave a Comment