Python Tutorial @ Home

Python Exception Handling

Python Exception
Common Exceptions
The problem without handling exceptions
Exception handling in python
The except statement with no exception
The except statement using with exception variable
Declaring Multiple Exceptions
The try…finally block
Raising exceptions

Python Exception Handling-8

Example:
Write a python program to take input for three numbers, check and print the largest number?
conditions:
numbers should be >=0

try:

    a=int(input("Enter 1st no "))
    b=int(input("Enter 2nd no "))
    c=int(input("Enter 3rd no "))
    if(a<0):
        raise ValueError("First Number is Invalid")
    if(b<0):
        raise ValueError("Second Number is Invalid")
    if(c<0):
        raise ValueError("Third Number is Invalid")
    if(a>b and a>c):
        m=a
    elif(b>c):
        m=b
    else:
        m=c
    print("Max no = ",m)
except ValueError as e:
    print(e)

Output:
Case:1
Enter 1st no 10
Enter 2nd no 25
Enter 3rd no 63
Max no = 63
Case:2
Enter 1st no 25
Enter 2nd no 63
Enter 3rd no -95
Third Number is Invalid

Example:
Write a python program to take input for three numbers, check and print the lowest number?
conditions:
numbers should be >=0

try:

    a=int(input("Enter 1st no "))
    b=int(input("Enter 2nd no "))
    c=int(input("Enter 3rd no "))
    if(a<0):
        raise ValueError("First Number is Invalid")
    if(b<0):
        raise ValueError("Second Number is Invalid")
    if(c<0):
        raise ValueError("Third Number is Invalid")
    if(a<b and a<c):
        m=a
    elif(b<c):
        m=b
    else:
        m=c
    print("Min no = ",m)
except ValueError as e:
    print(e)

Output:
Case:1
Enter 1st no 25
Enter 2nd no -95
Enter 3rd no 34
Second Number is Invalid
Case:2
Enter 1st no 14
Enter 2nd no 52
Enter 3rd no 57
Min no = 14

Example:
Write a python program to take input for student details like roll,name,m1,m2,m3. Calculate and print total and per.
Condition:
Roll number should be a 3 digit no
M1,m2 and m3 should be >=0 and <=100

code

Example:
Write a python program to take input for employee like empno, name and salary. Further display the details.
Condition:
empno should be a 4 digit no
salary should be in 5 digits

try:

    empno=int(input("Enter empno "))
    name=input("Enter name ")
    sal=int(input("Enter salary "))
    if not (empno>=1000 and empno<=9999):
        raise ValueError("Invalid empno, should be 4 digit")
    if not (sal>=10000 and sal<=99999):
        raise ValueError("Invalid Salary, should be 5 digit")
    print("Employee Details")
    print("Empno ",empno)
    print("Name ",name)
    print("Salary ",sal)
except ValueError as e:
    print(e)
    

Output:
Case:1
Enter empno 101
Enter name Amit
Enter salary 45000
Invalid empno, should be 4 digit
Case:2
Enter empno 1001
Enter name Sumit
Enter salary 4500
Invalid Salary, should be 5 digit
Case:3
Enter empno 1001
Enter name Kishan
Enter salary 54000
Employee Details
Empno 1001
Name Kishan
Salary 54000

Visitor Counter

0056749