The total concept of will Try and Exception statements in Python

try and Exception in Python
  • Post author:

In this article, you will be able to learn to try and Exceptions in Python and the benefits of using it. Stay here to gain more unique knowledge of try-except in python.

Key points of this content –

Handling Exception

The working area of try and exception statements

Catching Exception

Try Else Exception

Try Finally Exception

What is a Try and Exception statement in Python?

Python includes many built-in statements that occur when any coding encounters errors or unexpected things.

It works automatically when python coding shows any errors at that point you run a program.

Python try block helps to check block code errors and Exception block helps to maintain these errors. 

When an error occurs in the running program that is called an exception.

Python Exception handling with try statements

If your including program occurs any errors, at that point python automatically generates an errors message using try statements. 

Example of try statements

# The try block will provide an error message because it is not defined 


print ( t )


print(“An exception occurred”)

The output of this –

Working process Python Try and Exception

First of all, you need to understand how the try and Exception statements in Python work. Now I share the working procedure in python Try and Exception here.

  1. When both try and Exception apparel at a time, that point try close is executed.
  2. When any exception occurs here except close will run and try close will skip its position.
  3. If an exception occurs in any statements but doesn’t handline any code, at that point, it passes try statements. And except it is unhandling then it stops.
  4. Try statements can maintain more than one exception and more areas.

Example of try and Except working process –

# python code

# working process of try ( )

def divide ( a, b ) :


# floor division: Provides only Fractional

# Part a answer result = a / / b

print ( “ Yes! Your answer is: ”, result) 

Except for ZerodivisionError :

print (“Sorry! You are dividing by Zero”)

# find out parameters and note down the working of the program is –

Divided (3, 2)

Divided (3, 0)

The output of the codes –

Yes! Your answer is: 1

Sorry! You are dividing by Zero

Catching Exceptions in Python

You can handle any exception in python just using try statements. When python area codes occur any critical operation that can raise an exception is placed inside the try statements. It handles the exception as written in the except statements.

Example of catching exception –

# import statements sys to get the type of except

Import sys 

randomList = [ ‘x’, 0, 5 ]

for entry in randomList:

try : 

print ( “The entry is”, entry)

s = 1/ int (entry)


except :

print( “ Oops!”, sys.exc info () [ 0 ], “occurred.”)

print( “Next entry.”)


print ( “The reciprocal of ”, entry, “is”, s)

Try Else Exception statements in Python

When no errors are raised, at that point you can use the else keywords to indicate the block of code exception. You can use the else close with the try statements.

Example of Else Exception –

#The try block does not raise any errors, so the else block is executed:


print(” Hello, WigMarketing”)


  print(“Something went wrong”)


  print(“Nothing went wrong”)

The output of these codes-

Hello, WigMarketing

Nothing went wrong

Try Finally Exception close in Python

Try… finally is an optional close in Python. When the try statements raise or not the final statements will be executed. It can be used to clean up the resources before the program comes successfully run or not. 

Example of Finally Except –

# the try-finally block gets executed that does not matter when the try block raises any errors or is not executed


print( s )


print( “ Something went wrong”)


print(“The ‘try except’ is the End”)

The output of codes –

Something went wrong

The ‘try except’ is the End

In this article, you will be able to learn more about the total process of Try and Exception statements in Python.

It helps you to use these statements to close any kind of legal or valid Python code with it. Stay here to learn about total Basic to Advanced Python programming languages.

Have any questions related to the topic just drop a comment below the box. I will be back as soon as possible for your help.

Leave a Reply