Try with Else Clause Example in Python

By IncludeHelp Last updated : February 7, 2024


To understand the below example, you should have the basic knowledge of the following Python topics:

Try with Else Clause Example

The Python code below shows an example of try with clause example:

# Python program to demonstrate an example of
# try with clause example

# Function to divide two numbers
def divideFun(x, y):
        result = x / y
    except ZeroDivisionError:
        print("Error Python : [ZeroDivisionError: division by zero]")
        print("Result :", result)

# Main code
# Calling the function
divideFun(10, 3)
divideFun(10, 0)
divideFun(1, 2)


The output of the above example is:

Result : 3.3333333333333335
Error Python : [ZeroDivisionError: division by zero]
Result : 0.5

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.