×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

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
 
 
Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.