Nearby lessons

73 of 108

Python - Finally Block

Detailed Tutorial Notes

5) except ZeroDivisionError:
6) print("ZeroDivisionError:Can't divide with zero")
7) except:
8) print("Default Except:Plz provide valid input only")

9)

10) D:\Python_classes>py test.py
11) Enter First Number: 10
12) Enter Second Number: 0
13) ZeroDivisionError:Can't divide with zero

14)

15) D:\Python_classes>py test.py
16) Enter First Number: 10
17) Enter Second Number: ten
18) Default Except:Plz provide valid input only
  • **Note: If try with multiple except blocks available then default except block should be last,otherwise we will get SyntaxError. Eg:
1) try:
2) print(10/0)
3) except:
4) print("Default Except")
5) except ZeroDivisionError:
6) print("ZeroDivisionError")

7)

8) SyntaxError: default 'except:' must be last

Note:

The following are various possible combinations of except blocks

  • except ZeroDivisionError:
  • except ZeroDivisionError as msg:
  • except (ZeroDivisionError,ValueError) :
  • except (ZeroDivisionError,ValueError) as msg:
  • except : finally block:
  • It is not recommended to maintain clean up code(Resource Deallocating Code or Resource Releasing code) inside try block because there is no guarentee for the execution of every statement inside try block always.
  • It is not recommended to maintain clean up code inside except block, because if there is no exception then except block won't be executed. Hence we required some place to maintain clean up code which should be executed always irrespective of whether exception raised or not raised and whether exception handled or not handled. Such type of best place is nothing but finally block. Hence the main purpose of finally block is to maintain clean up code. try: Risky Code except: Handling Code finally: Cleanup code The speciality of finally block is it will be executed always whether exception raised or not raised and whether exception handled or not handled. Case-1: If there is no exception
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
try
finally

Case-2: If there is an exception raised but handled:

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
try
except
finally

Case-3: If there is an exception raised but not handled:

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
try
finally
ZeroDivisionError: division by zero(Abnormal Termination)
  • ** Note: There is only one situation where finally block won't be executed ie whenever we are using os._exit(0) function. Whenever we are using os._exit(0) function then Python Virtual Machine itself will be shutdown.In this particular case finally won't be executed.
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
try

Note:

os._exit(0)

where 0 represents status code and it indicates normal termination

There are multiple status codes are possible.

Control flow in try-except-finally:

try:

stmt-1

stmt-2

stmt-3

except:

stmt-4

🧠 Test Your Knowledge

5 Questions

Progress: 0 / 5
Keep Going!Python - Raise Exception