0% found this document useful (0 votes)
1 views

answerkey_exception

The document provides an overview of exception handling in Python, detailing key concepts such as throwing and catching exceptions, as well as the use of raise and assert statements. It explains the structure of try-except blocks and the importance of finally blocks in ensuring code execution regardless of exceptions. Additionally, it distinguishes between syntax errors and exceptions, emphasizing that while all syntax errors are exceptions, not all exceptions are syntax errors.

Uploaded by

deekshas5728
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

answerkey_exception

The document provides an overview of exception handling in Python, detailing key concepts such as throwing and catching exceptions, as well as the use of raise and assert statements. It explains the structure of try-except blocks and the importance of finally blocks in ensuring code execution regardless of exceptions. Additionally, it distinguishes between syntax errors and exceptions, emphasizing that while all syntax errors are exceptions, not all exceptions are syntax errors.

Uploaded by

deekshas5728
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Exception Handling in python

I. Answer the following


1. Exception Handling — The process of writing additional code in a program
to give proper messages or instructions to the user upon encountering an
exception is known as exception handling.
2. Throwing an exception — Throwing an exception refers to the
process of creating an exception object and passing it to the
runtime system or the appropriate exception handler.
3. Catching an exception — Catching an exception refers to the
process of executing a suitable handler or block of code
specifically designed to handle that particular exception when it
occurs during program execution.
4. It is raised when the result of a calculation exceeds the maximum limit for
numeric data type.
5. Syntax error. Indentation error
II. Answer the following
1.Raise and Assert statements are used to forcefully raise an exception in the
program.
Syntax:
Raise statement-raise exception-name[(optional argument)]
Assert statement-assert Expression[,arguments]
2. try:
numerator=50
denom=int(input("Enter the denominator"))
quotient=(numerator/denom)
print(quotient)
print ("Division performed successfully")
except ZeroDivisionError:
print ("Denominator as ZERO.... not allowed")
The part of the program that is expected to raise an exception is written
inside the try block .The code to handle such exceptions raised are written
within except block.
3. A syntax error is a specific type of exception that is detected when we have
not followed the rules of the particular programming language while writing a
program. On the other hand, an exception is a Python object that represents
any type of error or exceptional condition encountered during program
execution. This includes not only syntax errors but also runtime errors and
logical errors. Therefore, every syntax error is an exception but every
exception cannot be a syntax error.
III. Answer the following
1. The statements inside the finally block are always executed, regardless of
whether an exception has occurred in the try block or not. It is a common
practice to use the finally clause while working with files to ensure that the
file object is closed.
2. try:
3. num1 = int(input("Enter the first number: "))
4. num2 = int(input("Enter the second number: "))
5. quotient = (num1 / num2)
6. print(quotient)
7. print("Both numbers entered were correct")
8. except ValueError:
9. print("Please enter only numbers")
10. except ZeroDivisionError:
11. print("Number 2 should not be zero")
12. else:
13. print("Great.. you are a good programmer")
14. finally:
15. print("JOB OVER... GO GET SOME REST")

2. import math
try:
result = math.pow(2, 3, 4, 5) # pow() expects 2 arguments,
# but 4 are provided
except TypeError:
print("TypeError occurred with math.pow()")
else:
print("Result:", result)
3. numerator = float(input("Enter the numerator: "))
denominator = float(input("Enter the denominator: "))
if denominator == 0:
raise ZeroDivisionError("Error: Denominator cannot be zero.")
else:
quotient = numerator / denominator
print("Quotient:", quotient)

Answer the following

1.

You might also like