Computer Science
Answer
An exception is said to be caught when a code that is designed to handle a particular exception is executed. Exceptions, if any, are caught in the try block and handled in the except block. While writing or debugging a program, a user might doubt an exception to occur in a particular part of the code. Such suspicious lines of codes are put inside a try block. Every try block is followed by an except block. The appropriate code to handle each of the possible exceptions (in the code inside the try block) are written inside the except clause. While executing the program, if an exception is encountered, further execution of the code inside the try block is stopped and the control is transferred to the except block. The syntax of try … except clause is as follows:
try:
[ program statements where exceptions might occur]
except [exception-name]:
[ code for exception handling if the exception-name error is encountered]
Related Questions
Use assert statement in Question No. 3 to test the division expression in the program.
Define the following:
- Exception Handling
- Throwing an exception
- Catching an exception
Consider the code given below and fill in the blanks.
print("Learning Exceptions...") try: num1 = int(input("Enter the first number")) num2 = int(input("Enter the second number")) quotient = (num1/num2) print("Both the numbers entered were correct") except ...............: # to enter only integers print("Please enter only numbers") except ...............: # Denominator should not be zero print("Number 2 should not be zero") else: print("Great .. you are a good programmer") ...............: # to be executed at the end print("JOB OVER... GO GET SOME REST")You have learnt how to use math module in Class XI. Write a code where you use the wrong number of arguments for a method (say sqrt() or pow()). Use the exception handling process to catch the ValueError exception.