codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

47 / 68 topics
45Python File Handling (Read/Write/Delete)46Reading & Writing CSV Files47Python Exception Handling48Python Custom Exceptions
Tutorials/Python Programming/Python Exception Handling
🐍Python Programming

Python Exception Handling

Updated 2026-04-20
4 min read

Python Exception Handling

Exception handling is a crucial aspect of robust software development, allowing developers to manage and respond to errors gracefully without crashing the program. In this section, we will delve into the intricacies of exception handling in Python, covering how to handle exceptions, create custom exceptions, and implement best practices for error management.

Understanding Exceptions

An exception is an event that disrupts the normal flow of a program’s instructions. When an error occurs, Python raises an exception, which can be caught and handled by the programmer. If left unhandled, exceptions will terminate the program, potentially leading to data loss or other issues.

Common Built-in Exceptions

Python provides several built-in exceptions that cover a wide range of error scenarios:

  • SyntaxError: Raised when there is a syntax error in the code.
  • NameError: Raised when a variable is not found in the local or global scope.
  • TypeError: Raised when an operation or function is applied to an object of inappropriate type.
  • ValueError: Raised when a function receives an argument of the correct type but inappropriate value.
  • IndexError: Raised when trying to access an index that is out of range for a sequence (like a list or tuple).
  • KeyError: Raised when a dictionary key is not found.

Basic Exception Handling with try and except

The basic structure for handling exceptions in Python involves using the try and except blocks. The code that might raise an exception is placed inside the try block, while the code to handle the exception is placed inside the except block.

# Example of basic try-except block
try:
    result = 10 / 0
except ZeroDivisionError as e:
    print(f"An error occurred: {e}")

In this example, attempting to divide by zero raises a ZeroDivisionError, which is caught and handled by the except block.

Handling Multiple Exceptions

You can handle multiple exceptions in a single try-except block by specifying them as a tuple:

# Example of handling multiple exceptions
try:
    number = int(input("Enter a number: "))
    result = 10 / number
except (ValueError, ZeroDivisionError) as e:
    print(f"An error occurred: {e}")

Here, both ValueError and ZeroDivisionError are caught by the same except block.

Using else and finally Blocks

The try statement can also include else and finally blocks:

  • else Block: Code inside this block will execute if no exceptions were raised in the try block.
  • finally Block: Code inside this block will always execute, regardless of whether an exception was raised or not. This is typically used for cleanup actions.
# Example of try-except-else-finally blocks
try:
    number = int(input("Enter a number: "))
    result = 10 / number
except ValueError as e:
    print(f"ValueError occurred: {e}")
except ZeroDivisionError as e:
    print(f"ZeroDivisionError occurred: {e}")
else:
    print(f"The result is {result}")
finally:
    print("Execution completed.")

In this example, the else block executes if no exceptions are raised, and the finally block always prints a completion message.

Creating Custom Exceptions

You can define your own custom exceptions by inheriting from Python’s built-in Exception class. This is useful for creating more specific error messages or handling errors in a unique way.

# Example of creating a custom exception
class MyCustomError(Exception):
    def __init__(self, message):
        super().__init__(message)

try:
    raise MyCustomError("This is a custom error.")
except MyCustomError as e:
    print(f"Caught an exception: {e}")

In this example, MyCustomError is a custom exception that inherits from the base Exception class. It can be raised and caught like any other exception.

Best Practices for Exception Handling

  1. Catch Specific Exceptions: Always catch specific exceptions rather than using a generic except clause to avoid masking unexpected errors.
  2. Use else Block Wisely: The else block is useful for code that should only run if no exceptions were raised, making your code more readable and maintainable.
  3. Avoid Overusing Exceptions: Use exceptions for exceptional conditions, not for normal control flow. For example, don’t use exceptions to handle missing files; instead, check the file existence first.
  4. Log Exceptions: Use logging to record exceptions rather than just printing them. This is especially important in production environments where you need to track errors over time.
  5. Clean Up Resources with finally: Ensure that resources like files or network connections are properly closed using the finally block.

Real-World Example: File Handling with Exception Handling

Here’s a comprehensive example of handling file operations with exception handling:

# Example of file handling with exception handling
def read_file(file_path):
    try:
        with open(file_path, 'r') as file:
            content = file.read()
            print(content)
    except FileNotFoundError as e:
        print(f"File not found: {e}")
    except IOError as e:
        print(f"An I/O error occurred: {e}")
    finally:
        print("Finished attempting to read the file.")

# Usage
read_file('example.txt')

In this example, the read_file function attempts to open and read a file. It handles FileNotFoundError if the file does not exist and IOError for other input/output errors. The finally block ensures that a completion message is printed regardless of whether an exception was raised.

Conclusion

Exception handling is a powerful feature in Python that allows you to write more robust and reliable code. By understanding how to catch, handle, and create custom exceptions, you can manage errors effectively and ensure your programs behave predictably even in the face of unexpected issues. Always follow best practices to maintain clean and effective exception handling in your Python applications.


PreviousReading & Writing CSV FilesNext Python Custom Exceptions

Recommended Gear

Reading & Writing CSV FilesPython Custom Exceptions