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

Python Exception Handling

The document outlines the types of built-in exceptions in Python, such as SyntaxError, TypeError, and ZeroDivisionError, along with their meanings. It explains the use of try-except blocks for handling exceptions, including examples of catching multiple exceptions, using finally and else blocks. Additionally, it discusses the advantages of try-except, such as preventing crashes and enhancing debugging, as well as disadvantages like potential performance overhead and increased code complexity.

Uploaded by

yoeurthsaiyann20
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)
2 views

Python Exception Handling

The document outlines the types of built-in exceptions in Python, such as SyntaxError, TypeError, and ZeroDivisionError, along with their meanings. It explains the use of try-except blocks for handling exceptions, including examples of catching multiple exceptions, using finally and else blocks. Additionally, it discusses the advantages of try-except, such as preventing crashes and enhancing debugging, as well as disadvantages like potential performance overhead and increased code complexity.

Uploaded by

yoeurthsaiyann20
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/ 4

Types of Exceptions and Try-Except in Python

1. Types of Exceptions in Python


Python provides several built-in exceptions that can be categorized as follows:

1.1 Common Built-in Exceptions


SyntaxError – Raised when there is a syntax mistake in Python code.
TypeError – Raised when an operation or function is applied to an object of inappropriate type.
ValueError – Raised when a function gets an argument of the right type but inappropriate value.
IndexError – Raised when trying to access an index that is out of range.
KeyError – Raised when a dictionary key is not found.
ZeroDivisionError – Raised when dividing by zero.
AttributeError – Raised when an invalid attribute reference is made.
IOError – Raised when an input/output operation fails.
FileNotFoundError – Raised when trying to open a file that does not exist.

2. Try-Except (Try-Catch) in Python


The
try-except
block in Python is used for handling exceptions.

2.1 Basic Try-Except Example


try:
x = 10 / 0 # This will raise ZeroDivisionError
except ZeroDivisionError:
print("Cannot divide by zero!")

2.2 Catching Multiple Exceptions

try:
a = int("abc") # This will raise ValueError
b = [1, 2, 3]
print(b[5]) # This will raise IndexError
except (ValueError, IndexError) as e:
print(f"An error occurred: {e}")

2.3 Using Finally Block

try:
file = open("test.txt", "r")
except FileNotFoundError:
print("File not found!")
finally:
print("Execution completed.")

2.4 Using Else Block


try:
num = int(input("Enter a number: "))
print(f"You entered: {num}")
except ValueError:
print("Invalid input, please enter a number.")
else:
print("Execution successful.")

3. Advantages and Disadvantages of Try-Except

Advantages:
Prevents program crashes – Handles unexpected errors gracefully.
Enhances debugging – Helps identify and handle exceptions properly.
Improves user experience – Provides friendly error messages instead of program termination.
Ensures resource management – Allows proper handling of files, database connections, etc.

Disadvantages:

Overuse can hide bugs – Catching all exceptions might suppress critical errors.
Performance overhead – Handling exceptions is slower than regular code execution.
Complexity – Nested try-except blocks can make code harder to read and maintain.
Python Exception Handling Tutorial

You might also like