ExceptionsHandling
ExceptionsHandling
Error: Error refers to any deviation from the expected behavior or rules of the language, resulting in a
program not working as intended or crashing.
Types of Error
1. Compile-time errors: These are the errors resulting out of violation of programming language’s
grammar rules. All syntax errors are reported during compilation.
2. Run-time errors: The errors that occur during runtime because of unexpected situations. Such
errors are handled through exception handling routines of Python
Syntax Error: These are the errors resulting out of violation of programming language’s grammar rules
Exception: An exception is an unwanted or unexpected event that occurs during the execution of a
program (i.e., at runtime). These events can lead to program termination or incorrect result.
try Block: try block lets us test a block of code for errors. Python will “try” to execute the code
in this block. If an exception occurs, execution will immediately jump to the except block.
except Block: except block enables us to handle the error or exception. If the code inside the
try block throws an error, Python jumps to the except block and executes it. We can handle
specific exceptions or use a general except to catch all exceptions.
else Block: else block is optional and if included, must follow all except blocks. The else block
runs only if no exceptions are raised in the try block. This is useful for code that should execute
if the try block succeeds.
finally Block: finally block always runs, regardless of whether an exception occurred or not. It
is typically used for cleanup operations (closing files, releasing resources)
Explanation:
1. try block asks for user input and tries to divide 100 by the input number.
4. finally block runs regardless of the outcome, indicating the completion of execution.
Catching Exceptions
2. Using multiple except block: Sometimes, a single piece of code might be suspected to have
more than one type of error. For handling such situations, we can have multiple except blocks
4. Catching multiple exception: We can catch multiple exceptions in a single block if we need to
handle them in the same way
Raise exception: The error detected may be a built-in exception or may be a user-defined one. raise
statement can be used to throw/raise user defined exceptions.
Questions:
1. “Every syntax error is an exception but every exception cannot be a syntax error.” Justify
the statement.
2. When are the following built-in exceptions raised? Give examples to support your
answers.
a) ZeroDivisionError
b) NameError
c) IOError
d) ImportError
3. What is the use of a raise statement? Write a code to accept two numbers and display
the quotient. Appropriate exception should be raised if the user enters the second
number (denominator) as zero (0).
4. Use assert statement in Question No. 3 to test the division expression in the program.
5. Define the following:
a) Exception Handling
b) Throwing an exception
c) Catching an exception
6. Explain catching exceptions using try and except block
7. Consider the code given below and fill in the blanks