Python - Python Advanced - Exceptional Handling Tutorial
Python has many built-in exceptions or errors that are raised when in your program something goes wrong.
When these exceptions occur python program will get crash/terminate in between, which will stop the execution of the rest of the program and generate an error message.
This exception is handled using a try statement, so whenever the exception occurs in between, it doesn't stop the execution of the rest of the program.
Types of error-
Python Program ---> Compile(Compile time error) ----> Interpreter(Runtime error)
1. Compile time error
A compile-time error in a program is an error that occurs at the compile time, these errors are detected by the compiler at the same time and thus are known as compile-time errors.
Example - syntax, indentation
Example 1 to demonstrate Compile Time Error-
a = 3
print(a
Output-
Input In [1]
print(a
^
SyntaxError: unexpected EOF while parsing
Example 2-
a = 3
if(a==3):
print("Incorrect Indentation")
print("Correct Indentation")
Output-
Input In [6]
print("Correct Indentation")
^
IndentationError: unexpected indent
2. logical error - an error made by humans
3. Runtime error
A runtime error in a program is an error that occurs while the program is running after being successfully compiled
Example - divide by zero, namer error, a type error
Example to demonstrate Runtime error-
a = 3
print(a/0)
Output-
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
Input In [7], in <cell line: 2>()
1 a = 3
----> 2 print(a/0)
ZeroDivisionError: division by zero
Example 2-
a = [3,4,5]
print(a[3])
print("Hello")
Output-
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Input In [9], in <cell line: 2>()
1 a = [3,4,5]
----> 2 print(a[3])
4 print("Hello")
IndexError: list index out of range
In the above program, the rest of the program i.e print("Hello") will not run. because the exception has occurred on the print(a[3]) line. We will learn to handle this error in the upcoming tutorial.
List Of Errors-
# list of errors
print(dir(__builtins__))
Output-
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError',
'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError',
'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning',
'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError',
'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError',
'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError',
'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError',
'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented',
'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError',
'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError',
'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning',
'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError',
'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError',
'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError',
'__IPYTHON__', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__',
'__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray',
'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr',
'dict', 'dir', 'display', 'divmod', 'enumerate', 'eval', 'exec', 'execfile', 'filter', 'float',
'format', 'frozenset', 'get_ipython', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id',
'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map',
'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property',
'range', 'repr', 'reversed', 'round', 'runfile', 'set', 'setattr', 'slice', 'sorted',
'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
Exception handling will only handle runtime errors.
Keywords that will be used for exceptional handling-
1. try
2. except
3. else
4. finally
5. raise
1] Try and Except
Try - Only the code which will be going to raise an error should be written in the try block.
Except - Alternative code used to handle the error, if the error is raised by the try block
Syntax-
try:
# logic- in which error can occur
except:
# Alternate code to handle try block
Example-
num1 = eval(input('enter the 1st numbers'))
num2 = eval(input('enter the 2nd numbers'))
try:
c = num1 / num2
print(c)
except:
print('Error Occured')
print('Hello')
Output-
enter the 1st numbers3
enter the 2nd numbers0
Error Occured
Hello
In the above program, on dividing 3 by 0, an error has occurred, which is catch by the try block and handled by except block.
After an exception is handled, the program will not get terminated, It will still run print("Hello").
To print, an exception occur i.e except Exception as e -
Exception as a message is mostly used by developers to see the error message. For users, we can simply print the normal message in print such as - "Error Occured" or anything else instead of showing other all errors.
Other all error means, suppose while dividing two numbers a with b if b is 0 (zero). Then it will throw ZeroDivisionError.
And if the user enters the alphabetic character as input for b, then it will throw NameError or any other error(E.g ValueError).
In the frontend part, we can prevent a user from entering the alphabet as input, but we cannot prevent a user from entering 0.
So, in this case, we should handle only the exception/error that is required. i.e we should handle only ZeroDivisionError in this case(given in Example 2)
Example 1-
num1 = eval(input('enter the 1st numbers'))
num2 = eval(input('enter the 2nd numbers'))
try:
c = num1 / num2
print(c)
except Exception as e:
print('Error Occured')
print(e)
print('Hello')
Output-
enter the 1st numbers4
enter the 2nd numbers0
Error Occured
division by zero
Hello
The above example, It is showing that the "division by zero" error has occurred.
Example 2-
num1 = input('enter the 1st numbers')
num2 = input('enter the 2nd numbers')
try:
c = int(num1) / int(num2)
except ZeroDivisionError as msg:
print(msg)
else:
print(c)
Output 1(on putting b as 0)-
enter the 1st numbers3
enter the 2nd numbers0
division by zero
Output 1(on putting b as an alphabetic character)-
enter the 1st numbers3
enter the 2nd numbersa
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [22], in <cell line: 4>()
2 num2 = input('enter the 2nd numbers')
4 try:
----> 5 c = int(num1) / int(num2)
6 except ZeroDivisionError as msg:
7 print(msg)
ValueError: invalid literal for int() with base 10: 'a'
In the above program, we only handle specific ZeroDivisionError, Other errors we don't handle.
2] Using else with try and except
else - else block will only when there is no error occurred in the try block
Syntax-
try:
# logic- in which error can occur
except:
# Alternate code to handle try block
else:
# exec if no error in the try block
Example-
num1 = eval(input('enter the 1st numbers'))
num2 = eval(input('enter the 2nd numbers'))
try:
c = num1 / num2
except Exception as e:
print(e)
else:
print(c)
Output 1(when num1 = 3 and num2 = 0)-
enter the 1st numbers3
enter the 2nd numbers0
division by zero
In the above output except block has catch the error that occurred in a try block
Output 2(when num1 = 3 and num2 = 3)-
enter the 1st numbers3
enter the 2nd numbers3
1.0
In the above output else block is executed as there is no exception occurred on try block
3] Finally with try and catch block
finally - it will execute no matter what happens to try block. If the try block has raised an error, and except the block has not caught the error, still finally block will get executed. Example with catch block-
num1 = eval(input('enter the 1st numbers'))
num2 = eval(input('enter the 2nd numbers'))
try:
c = num1 / num2
except Exception as e:
print(e)
finally:
print("finally block will execute anyway, regardless of try block")
# normal block
print("Normal Code")
Output-
enter the 1st numbers3
enter the 2nd numbers0
division by zero
finally block will execute anyway, regardless of try block
Normal Code
In the above program both finally block and normal code is executed, regardless of the exception that has occurred
Example without catch block-
num1 = eval(input('enter the 1st numbers'))
num2 = eval(input('enter the 2nd numbers'))
try:
c = num1 / num2
finally:
print("finally block will execute anyway, regardless of try block")
# normal block
print("Normal Code")
Output-
enter the 1st numbers3
enter the 2nd numbers0
finally block will execute anyway, regardless of try block
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
Input In [23], in <cell line: 4>()
2 num2 = eval(input('enter the 2nd numbers'))
4 try:
----> 5 c = num1 / num2
6 finally:
7 print("finally block will execute anyway, regardless of try block")
ZeroDivisionError: division by zero
In the above program, only the finally block is executed, the normal block is not executed
Nested Try and except
Try or Except block inside the other Try or Except block is called a Nested Try and Except block.
Example-
a = 10
try: #try
b = input('please enter the value ')
try: #trytry
c = a/int(b)
print(c)
except ZeroDivisionError as e: #tryexcept
print('ZeroDivsionError - #trytry Error handled by #tryexcept inside #try')
except: #except
print('#try error handled by #except')
try: #excepttry
print(b[4])
except Exception as e: #exceptexcept
print('IndexOutOfRangeError - #try Error handled by #exceptexcept inside #except')
Output 1 (if no error, then #trytry will get executed and the result will get printed)-
please enter the value 5
2.0
Output 2 (if divided by zero, ZeroDivsionError has occurred in #trytry and handled by #tryexcept)-
please enter the value 0
ZeroDivsionError - #trytry Error handled by #tryexcept inside #try
Output 3 (if the inputted value is a string of more than 4 characters, then it will move toward #except for block ->#excepttry block)-
please enter the value fresherbell
#try error handled by #except
h
Output 4 (if the inputted value is a string of more than 4 characters, then it will move toward #except for block ->#exceptexcept block)-
please enter the value fre
#try error handled by #except
IndexOutOfRangeError - #try Error handled by #exceptexcept inside #except
User Defined Error
Error defined by the user for the required condition to which the user wants to handle the exception. Suppose the user wants to raise an error, whenever the user's age is below 18.
For Example-
# User Defined error
class AgeBelow18Error(Exception):
# Constructor or Initializer
def __init__(self, val):
self.val = val
# __str__ is to print() the message
def __str__(self):
return("AgeBelow18Error: "+str(self.val)+" is below 18")
age = int(input('enter the age'))
try:
if age < 18:
raise AgeBelow18Error(age)
else:
print('Valid Age')
except AgeBelow18Error as e:
print('Error below')
print(e)
Output 1 (if age is above 18)-
enter the age19
Valid Age
Output 2 (if age is below18)-
enter the age12
Error below
AgeBelow18Error: 12 is below 18