Total:
Exception Handling Assignment #1 /10
Name: __Saad Nazir_____ Date: ___May 17______
1. What is an exception? Explain. (1 mark)
An exception in the context of computer programming is an unexpected
event or error that occurs during the execution of a program. It's
like a hiccup that happens when the program encounters something it
doesn't know how to handle or when something goes wrong. Just like in
real life, when we encounter unexpected situations, the program can't
proceed as planned and needs to react accordingly.
When an exception occurs, it interrupts the normal flow of the program
and can cause it to terminate prematurely if not properly handled. To
prevent the program from crashing, we use exception handling
techniques. These techniques allow us to catch and deal with
exceptions gracefully, so the program can either recover from the
error or terminate in a controlled manner, providing useful
information about what went wrong.
In summary, an exception is an unexpected event or error that occurs
during program execution. It's a way for the program to communicate
that something went wrong and requires special attention. Exception
handling helps us deal with these unexpected situations and keeps our
programs running smoothly by providing alternative paths or gracefully
terminating when necessary.
2. Write the standard notation used in Python to handle an
exception? (2 mark)
In Python, we use a try-except block to handle exceptions. This block
helps us catch and manage errors that might occur during the execution
of our code. The basic structure looks like this:
try:
# Code that may cause an exception
# ...
except ExceptionType:
# Code to handle the exception
# ...
By using this try-except structure, we can gracefully handle
exceptions and prevent our program from crashing unexpectedly.
Sure, here are some standard exceptions used in Python:
: This exception is raised when a function receives an argument of the
correct type but an inappropriate value.
Remember that these are just a few examples of standard exceptions in
Python, and there are many more exceptions available for handling
various types of errors and exceptional situations in your code.
3. Use the organizer below to identify some of the standard
exceptions used in Python (Name only). (2 marks)
ValueError TypeError
OverflowError IndexError
KeyError
ZeroDivisionError
ImportError FileNotFound
Error
4. What is an assertion in Python? (1 mark)
In Python, an assertion is a way to check if a certain condition is
true or false during the execution of a program. It acts as a sanity
check, allowing you to ensure that the assumptions made in your code
hold true. When an assertion is used, you are basically stating that
something must be true at that point in your program, otherwise, an
error is raised.
For example, let's say you're writing a program that calculates the
average of a list of numbers. Before performing the calculation, you
can use an assertion to check if the list is not empty. If the list is
empty, the assertion will raise an AssertionError, indicating that
something unexpected has occurred.
5. What is the difference between assertion and exception handling
in Python? Use the Venn diagram below to answer. (4 marks)
Assertion Exception
Used to handle errors or
Used to verify certain
exceptional situations that occur
conditions in a program.
during program execution.
Typically used during
development and testing Provides a way to gracefully
phases to catch logical errors recover from errors and prevent
Error Detection
or assumptions. program crashes.
Helps in detecting and fixing issues Error Reporting
Generally used in production
early in the development process. environments to handle unexpected
Program Flow Control
situations.
Intended to be used for internal
consistency checks. Debugging aid
Helps in maintaining program
stability and user-friendly
Usually disabled in
interactions.
production environments
for performance reasons. Exception handling code is
structured using try-except
blocks.