Exceptions

In Python, it is very easy to perform operations such as division and file handling. However, sometimes errors might occur during the program's execution, causing the program to stop abruptly. This is where exception handling comes into play. Python uses try, except blocks to handle exceptions. The operations that can raise exceptions are placed inside the 'try' block, and the code to handle the exceptions are placed inside the except block.
Consider a simple division operation:
a = int(input())
b = int(input())
print(a / b)
print('The program continues...')
In the case of inputs 10 and 2, the program would print 5.0 and The program continues.... However, if we input 10 and 0, the program would stop and print an error message: ZeroDivisionError: division by zero.
To handle this error, we can use exception handling:
try:                       # Try to perform the operations in the block
    a = int(input())
    b = int(input())
    print(a / b)
except ZeroDivisionError:  # What to do if a ZeroDivisionError occurs
    print('Error: Division by zero is not allowed.')

print('The program continues...')
Now if we run the program with inputs 10 and 0, it will print Error: Division by zero is not allowed. and The program continues....
Similarly, we can handle other exceptions such as ValueError which occurs when the input is not an integer. We can add another except block for handling this error:
try:
    a = int(input())
    b = int(input())
    print(a / b)
except ZeroDivisionError:
    print('Error: Division by zero is not allowed.')
except ValueError:
    print('Error: Input is not a valid integer.')
We can add as many except blocks as we need to handle different types of exceptions. This way, we can ensure our program doesn't stop abruptly due to errors and can provide helpful messages to the user.
💡
It's important to note that the order of the except blocks matters. Python will execute the first except block that matches the type of error occurred. If none of the except blocks matches, the error will not be handled.
You can also catch multiple exceptions in one line:
try:
    a = int(input())
    b = int(input())
    print(a / b)
except (ZeroDivisionError, ValueError) as e:
    print('Error:', e)
Here, e will hold the instance of the exception raised.
By using exception handling, we can make our Python programs more robust and user-friendly.

Challenge: Area of a Rectangle

You are to write a program that reads two floating-point numbers from the input, these numbers h and w represent the height and the width of a rectangle. Your task is to calculate and print the area of the rectangle.
However, not all inputs are guaranteed to be valid numbers. If either h or w is not a number, you should handle the exception and print The input is not a number.
The first line of the input contains the height h and the second line contains the width w.
The program should print a single line with the area of the rectangle if the inputs are valid numbers or The input is not a number if one or both of the inputs are not valid numbers.
Input
Output
5.2 7.8
40.56
3.7 abc
The input is not a number
abc 5.5
The input is not a number
abc def
The input is not a number
Note: The input will contain only positive numbers in case they are numbers.
 

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue