Python try-except- else vs finally with example program

  • For detail explanation of try and except block please check below link
  • Python try and except blocks with an example program
  • We will place all statements which are proved to generate exceptions in try block.
  • If any exception occurred then exception block will handle the exceptions.
  • If no exception raised in try block then except block wont be executed and else block will be executed.
  • In this scenario we might move the code from else to try block. If any exception raised in try block other statements in try block wont be executed and also else block will not be executed.
  • Finally block will always executes irrespective of any kind of exceptions.
  • So if we place any statements inside finally block of python script then those those statements will be executed for sure.
  • Else block will be executed when no exception occurred in try and finally will executes irrespective of any exception.
  • Lets see an example python program on try-except-else and try except-else-finally.




#1: Write a Python program which explains usage both else and finally blocks in python programming.

  1. #try_except with both else and finally blocks:
  2. string=input("enter some string: ")

  3. try:
  4.     x=string[5]
  5.     print("char at index 5 is: ",x)
  6.     print("no exception")
  7. except IndexError as e:
  8.     print("exception raised: ",e)
  9. else:
  10.     print("else block is executing becoz of no exception")
  11. finally:
  12.     print("finally block will always executes")


Output

  1. enter some string: python
  2. char at index 5 is:  n
  3. no exception
  4. else block is executing becoz of no exception
  5. finally block will always executes


python else vs python finally

Python finally block example with program

  • Before discussing about finally block, please check our previous articles on try, except and else blocks. 
  • Basic python programs on try , except and else
  • As we already know about try like its duty is to find any exceptions if occurs and pass it to python except block so that except block will assign exception class object to handle exceptions.
  • Else will be executed if no exception occurs.
  • Finally block will executes even exceptions occurs. so in some situations we need to execute some statements compulsory even if any exceptions occurs in that scenario we will use python finally block
  • We will place all statements which would be executes irrespective of exceptions.
  • Lets see an example program on python finally block.
  • Python try except finally example
     



#1: Write a python program which explains usage of finally block in exception handling of python programming.

  1. try:
  2.   x = int(input("enter first number: "))
  3.   y = int(input("enter second number: "))
  4.   result=x/y
  5. except ArithmeticError as e:
  6.    print("cannot devide a number by zero: ", e)
  7. finally:
  8.    print("finally block")

Output
  1. enter first number: 10
  2. enter second number: 0
  3. cannot devide a number by zero:  division by zero
  4. finally block

finally block python

Python try except else with example program

  • Please read below post to get an idea of try and except blocks in detail
  • Python try except example program
  • We will place all statements which are proven to generate exceptions in try block so that if any error occurred it will immediately enters into except and assign exception object to corresponding class.
  • If any exception occurs in try then except will be executed in order to handle the exception.
  • If no exception occurred in the try the it will executes else block.
  • So in Python exception handing else block will be executed if and only if no exceptions are occurred in try block and all the statements in try executed.
  • Lets see an example program on how can we use else in python exception handling
  • try-except-else 




#1: Write a python example program which explains usage of else block in exception handling of python programming.

  1. try:
  2.     x = int(input("enter 1st number: "))
  3.     y = int(input("enter 2nd number: "))
  4.     print(x/y)
  5.     string=input("enter some string: ")
  6.     print(string[8])
  7. except (IndexError, ZeroDivisionError) as e:
  8.     print("An error occurred :",e)
  9. else:
  10.     print("no error")

Output

  1. enter 1st number: 2
  2. enter 2nd number: 0
  3. An error occurred : division by zero

  4. enter 1st number: 2
  5. enter 2nd number: 1
  6. 2.0
  7. enter some string: python
  8. An error occurred : string index out of range

  9. enter 1st number: 2
  10. enter 2nd number: 1
  11. 2.0
  12. enter some string: python is very easy
  13. s
  14. no error  


python else example program

Python try except example program

  • Exceptions are the objects representing the logical errors that occur at run time.
  • When python script encounters any error at run time it will create python object.
  • So we need to handle this situation otherwise python program will terminates because of that run time error ( Exception Object).
  • So we can handle this by assigning this python exception object to corresponding python class.
  • For that Python provides try and except blocks to handle exceptions.
  • In try block we need to keep all the statements which may raise exceptions at run time.
  • except block will catch that exception and assigns to corresponding error class.
  • Lets see an example python program on exception handling using try and except blocks.




#1: Example Program to handle exception using try and except blocks in python programming.

  1. #use of try_catch in functions:
  2. def f():
  3.    x=int(input("enter some number: "))
  4.    print(x)

  5. try:
  6.     f()
  7.     print("no exception")
  8. except ValueError as e:
  9.     print("exception: ", e)
  10.     print("Rest of the Application")

Output

  1. enter some number: 2
  2. 2
  3. no exception
  4. >>> 



  5. enter some number: "python"
  6. exception:  invalid literal for int() with base 10: '"python"'
  7. Rest of the Application
  8. >>> 

try and except python example
Select Menu