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

ExceptionsHandling

The document provides an overview of exception handling in Python, detailing the types of errors, including compile-time and run-time errors. It explains the structure of try-except blocks and the roles of try, except, else, and finally blocks in managing exceptions. Additionally, it covers catching exceptions, raising user-defined exceptions, and poses questions for further understanding of the concepts.

Uploaded by

vinod.1987k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

ExceptionsHandling

The document provides an overview of exception handling in Python, detailing the types of errors, including compile-time and run-time errors. It explains the structure of try-except blocks and the roles of try, except, else, and finally blocks in managing exceptions. Additionally, it covers catching exceptions, raising user-defined exceptions, and poses questions for further understanding of the concepts.

Uploaded by

vinod.1987k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Exception handling in Python

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.

Some python build-in exceptions


Exception Handling: Exception handling allows a program to gracefully handle unwanted or
unexpected events that occur during the execution of a program and recover from errors by taking
corrective actions instead of terminating the program.

In Python, exception handling is implemented using a try-except block.

try, except, else and finally Blocks

 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.

2. except blocks handle ZeroDivisionError and ValueError.

3. else block runs if no exception occurs, displaying the result.

4. finally block runs regardless of the outcome, indicating the completion of execution.
Catching Exceptions

1. Using try..except block

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

3. Using except without specifying an exception type

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

8. What is the use of finally clause? Write an example code


9. Can I use try without except in Python
10. In Python can a try have multiple except

You might also like