0% found this document useful (0 votes)
17 views1 page

CH - 6 Exception Handling

Exception handling in programming is essential for managing unexpected errors during execution, and Python offers a robust mechanism for this purpose. The `try-except` block allows developers to anticipate and handle exceptions, while the `finally` block ensures certain code executes regardless of errors. Key concepts include handling specific exceptions like division by zero and file not found errors, as well as using `raise` and `assert` to manage exceptions.

Uploaded by

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

CH - 6 Exception Handling

Exception handling in programming is essential for managing unexpected errors during execution, and Python offers a robust mechanism for this purpose. The `try-except` block allows developers to anticipate and handle exceptions, while the `finally` block ensures certain code executes regardless of errors. Key concepts include handling specific exceptions like division by zero and file not found errors, as well as using `raise` and `assert` to manage exceptions.

Uploaded by

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

Computer Science (XII) Notes by Sumit Garg Sir

EXCEPTION HANDLING

Exception handling is a crucial aspect of programming, aimed at gracefully managing and


recovering from unexpected errors that can occur during program execution. Errors can range from
simple typos to more complex issues like division by zero or attempting to access a nonexistent file.
Python provides a robust mechanism for handling exceptions to prevent program crashes and
improve code reliability.
Handling Exceptions Using Try-Except Blocks
The fundamental construct for handling exceptions in Python is the `try-except` block. It allows you
to define a section of code (the "try" block) where you anticipate exceptions might occur. If an
exception occurs within the "try" block, Python immediately jumps to the associated "except" block,
where you can define how to handle the exception.
Here's the syntax for a basic `try-except` block:
try:
# Code that may raise an exception
except ExceptionType:
# Code to handle the exception
- `try`: This block contains the code that might raise an exception.
- `except ExceptionType`: If an exception of the specified type occurs in the "try" block,
the code within the "except" block will execute to handle the exception.
Example 1: Handling Division by Zero
try:
numerator = 10
denominator = 0
result = numerator / denominator # This may raise a
ZeroDivisionError except ZeroDivisionError:
print("Error: Division by zero.")
Example 2: Handling File Not Found
try:
file = open("non_existent_file.txt", "r") # This may raise a FileNotFoundError
except FileNotFoundError:
print("Error: File not found.")
The `finally` Block
In addition to `try` and `except`, you can include a `finally` block. Code within the
`finally` block always executes, regardless of whether an exception was raised or not. It
is commonly used to perform cleanup operations, such as closing files or network
connections.
try:
# Code that may raise exceptions
except ExceptionType:
# Handle the exception
finally:
# Code that always executes
Example: Using `finally`
try:
file = open("sample.txt", "r")
content = file.read()
except FileNotFoundError:
print("Error: File not found.")
finally:
file.close() # Close the file, even if an exception occurred or not.
raise: to raise an exception forcefully assert: to raise an exception in absence of a condition

You might also like