Try with Else Clause Example in Python

By IncludeHelp Last updated : February 7, 2024

Prerequisite

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):
    try:
        result = x / y
    except ZeroDivisionError:
        print("Error Python : [ZeroDivisionError: division by zero]")
    else:
        print("Result :", result)

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

Output

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 www.includehelp.com. All rights reserved.