Q:

Python exception handling program (Handling divide by zero exception)

belongs to collection: Python Exception Handling Programs

0

Steps to handle type exception in python:

  • Step 1: We will take inputs from the user, two numbers.
  • Step 2: If the entered data is not integer, throw an exception.
  • Step 3: If the remainder is 0, throw divide by zero exception.
  • Step 4: If no exception is there, return the result.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to illustrate the type exception in python

try:
    num1 = int(input("Enter First Number: "))
    num2 = int(input("Enter Second Number: "))

    result = num1 / num2

    print(result)
except ValueError as e:
    print("Invalid Input Please Input Integer...")
except ZeroDivisionError as e:
    print(e)

Output:

Run 1: 
Enter First Number: 432.12
Invalid Input Please Input Integer...

Run 2: 
Enter First Number: 43
Enter Second Number: 0
division by zero

Run 3: 
Enter First Number: 331 2
Enter Second Number: 4
83.0

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python exception handling program (Handling Type e... >>