Skip to content

Latest commit

 

History

History
1195 lines (688 loc) · 14.4 KB

06-Exception-Handling-Best-Practices.md

File metadata and controls

1195 lines (688 loc) · 14.4 KB

Exception Handling Best Practices in Python

Table of Contents

  1. Learning Objectives
  2. Prerequisites
  3. Introduction
  4. Why Best Practices are Important
  5. Best Practices for Exception Handling
  6. Common Anti-Patterns
  7. Exception Logging
  8. Resource Management
  9. Production-Ready Exception Handling
  10. Internal Working
  11. Summary

Learning Objectives

After completing this chapter, you will be able to:

  • Write clean exception handling code.
  • Follow Python's recommended exception handling practices.
  • Avoid common exception handling mistakes.
  • Understand when and where to catch exceptions.
  • Learn basic exception logging.
  • Write production-ready Python programs.

Prerequisites

Before learning this chapter, you should know:

  • Exception Handling
  • try-except Blocks
  • else Block
  • finally Block
  • raise Keyword
  • User-Defined Exceptions

Introduction

Exception Handling is one of the most important aspects of software development.

Handling exceptions incorrectly can make programs:

  • Difficult to debug
  • Difficult to maintain
  • Less secure
  • Unreliable

Good exception handling makes applications:

  • Stable
  • Readable
  • Maintainable
  • User-friendly

Python provides powerful exception handling features,

but they should be used correctly.


Why Best Practices are Important?

Suppose a Banking Application catches every exception like this:

try:

    withdraw()

except:

    pass

If a serious error occurs,

the program silently ignores it.

The developer has no idea what went wrong.

Instead,

exceptions should be handled carefully,

logged when appropriate,

and meaningful messages should be displayed.


Best Practices for Exception Handling

Following these practices leads to cleaner and more reliable code.


1. Catch Specific Exceptions

Always catch the most specific exception possible.

✅ Good

except ValueError:

    print("Invalid input.")

❌ Bad

except:

    print("Error")

Specific exception handling makes debugging easier.


2. Keep try Blocks Small

Only include statements that may actually raise exceptions.

✅ Good

try:

    number = int(input())

except ValueError:

    print("Invalid Number")

Avoid placing unrelated code inside the try block.


3. Use Meaningful Error Messages

Bad messages make debugging difficult.

❌ Poor

Error

✅ Better

Age must be greater than or equal to 18.

4. Don't Ignore Exceptions

Avoid writing

except:

    pass

unless there is a valid reason.

Ignoring exceptions makes debugging difficult.


5. Use finally for Cleanup

Resources should always be released.

Examples include:

  • Files
  • Database Connections
  • Network Connections

Use

finally

to ensure cleanup.


6. Raise Exceptions When Necessary

Use

raise

to enforce business rules.

Example

raise ValueError("Invalid Age")

7. Create User-Defined Exceptions

When built-in exceptions are not descriptive enough,

create custom exceptions.

Example

class InsufficientBalanceError(Exception):

    pass

8. Log Exceptions

Instead of hiding errors,

record them.

Example

import logging

logging.error("Database Connection Failed")

Logging helps developers diagnose problems later.


9. Validate Input Before Processing

Always validate user input.

Example

if age < 18:

    raise ValueError("Invalid Age")

10. Separate Business Logic from Exception Handling

Keep validation and exception handling independent from the main business logic.

This improves readability and maintainability.


Common Anti-Patterns

Avoid the following practices.


Catching Every Exception

except:

Avoid this unless absolutely necessary.


Empty except Blocks

except:

    pass

This hides errors.


Using Exceptions for Normal Flow

Exceptions should represent exceptional situations,

not normal program behavior.


Raising Generic Exceptions

Prefer

ValueError

instead of

Exception

whenever possible.


Ignoring Cleanup

Always release resources using

finally

or a context manager (with statement).


Exception Logging

In production applications,

errors should be logged.

Example

import logging

logging.basicConfig(level=logging.ERROR)

try:

    print(10 / 0)

except ZeroDivisionError:

    logging.exception("Division Error")

Logging stores useful debugging information,

including the traceback.


Resource Management

Good

file = None

try:

    file = open("sample.txt")

finally:

    if file:

        file.close()

Even better,

use Python's

with

statement,

which automatically closes the file.

with open("sample.txt") as file:

    print(file.read())

Production-Ready Exception Handling

A production-ready application should:

  • Catch specific exceptions.
  • Log unexpected errors.
  • Display user-friendly messages.
  • Avoid exposing internal implementation details.
  • Release resources correctly.
  • Validate all external input.
  • Use custom exceptions for business rules.

Internal Working

Program Starts

↓

Input Validation

↓

Business Logic

↓

Exception Occurs?

↓

No

↓

Continue Program

------------------------

Yes

↓

Specific Exception Raised

↓

Matching Handler

↓

Log Exception

↓

Cleanup Resources

↓

Continue / Exit Program

Advantages

  • Cleaner code.
  • Easier debugging.
  • Better maintainability.
  • Improved application stability.
  • Better user experience.
  • Easier production support.

Interview Questions

1. Why should specific exceptions be caught?

Answer

Specific exceptions make debugging easier and prevent unrelated errors from being hidden.


2. Why should try blocks be kept small?

Answer

Small try blocks make it easier to identify which statement caused the exception.


3. Why is logging important?

Answer

Logging records exception details for debugging and production monitoring.


4. Why should except: pass be avoided?

Answer

Because it silently hides errors and makes debugging difficult.


5. Why is finally important?

Answer

It ensures that resources are released regardless of whether an exception occurs.


Quick Revision

  • Catch specific exceptions.
  • Keep try blocks small.
  • Use meaningful messages.
  • Avoid empty except blocks.
  • Use finally for cleanup.
  • Log important exceptions.
  • Validate inputs.
  • Use custom exceptions when needed.

Chapter Summary

In this chapter, we learned:

  • Exception Handling Best Practices
  • Exception Logging
  • Resource Management
  • Common Anti-Patterns
  • Production-Ready Exception Handling
  • Internal Working
  • Interview Questions
  • Quick Revision

Program 1 – Catching a Specific Exception

Problem Statement

Write a Python program to catch a specific exception instead of using a generic exception.


Program

try:

    number = int(input("Enter Number : "))

except ValueError:

    print("Please enter a valid integer.")

Sample Output

Enter Number : abc

Please enter a valid integer.

Explanation

Catching a specific exception makes debugging easier and prevents unrelated exceptions from being hidden.


Program 2 – Avoid Large try Blocks

Program

name = input("Enter Name : ")

try:

    age = int(input("Enter Age : "))

except ValueError:

    print("Invalid Age")

print("Name :", name)

Explanation

Only the risky statement is placed inside the try block.

The remaining code is kept outside.


Program 3 – Displaying Meaningful Error Messages

Program

try:

    marks = int(input("Enter Marks : "))

    if marks < 0 or marks > 100:

        raise ValueError(
            "Marks must be between 0 and 100."
        )

except ValueError as error:

    print(error)

Sample Output

Enter Marks : 120

Marks must be between 0 and 100.

Explanation

Meaningful error messages help users understand the exact problem.


Program 4 – Proper Resource Cleanup using finally

Program

file = None

try:

    file = open("sample.txt")

    print("File Opened")

except FileNotFoundError:

    print("File Not Found")

finally:

    if file:

        file.close()

        print("File Closed")

Explanation

The finally block ensures that the file is closed if it was successfully opened.


Program 5 – Resource Management using with Statement

Program

try:

    with open("sample.txt") as file:

        print(file.read())

except FileNotFoundError:

    print("File Not Found")

Explanation

The with statement automatically closes the file.

It is generally preferred over manually calling close().


Program 6 – Exception Logging

Program

import logging

logging.basicConfig(level=logging.ERROR)

try:

    print(100 / 0)

except ZeroDivisionError:

    logging.exception("Division Error")

Sample Output

ERROR:root:Division Error

Traceback (most recent call last):
...
ZeroDivisionError: division by zero

Explanation

Logging stores detailed error information, including the traceback.

This is useful for debugging production applications.


Program 7 – Input Validation Before Processing

Program

age = int(input("Enter Age : "))

if age < 18:

    raise ValueError(
        "Age must be at least 18."
    )

print("Registration Successful")

Sample Output

Enter Age : 15

ValueError: Age must be at least 18.

Explanation

Input is validated before further processing.


Program 8 – Using User-Defined Exception

Program

class InvalidSalaryError(Exception):

    pass

salary = int(input("Enter Salary : "))

if salary < 0:

    raise InvalidSalaryError(
        "Salary cannot be negative."
    )

Sample Output

Enter Salary : -1000

InvalidSalaryError:
Salary cannot be negative.

Explanation

Custom exceptions improve code readability by representing business rules.


Program 9 – Incorrect Exception Handling (Anti-Pattern)

Program

try:

    print(100 / 0)

except:

    pass

Explanation

This is not recommended.

The exception is silently ignored,

making debugging difficult.


Better Approach

try:

    print(100 / 0)

except ZeroDivisionError as error:

    print(error)

Program 10 – Production-Ready Exception Handling

Program

import logging

logging.basicConfig(level=logging.ERROR)

try:

    amount = int(input("Enter Amount : "))

    if amount <= 0:

        raise ValueError(
            "Amount must be greater than zero."
        )

    print("Transaction Successful")

except ValueError as error:

    logging.error(error)

    print("Invalid Transaction")

finally:

    print("Transaction Completed")

Sample Output

Enter Amount : -500

Invalid Transaction

Transaction Completed

Explanation

This example demonstrates:

  • Input Validation
  • Specific Exception Handling
  • Logging
  • Cleanup using finally

These are common practices in production applications.


Dry Run

Program Starts

↓

Input Accepted

↓

Validation

↓

Valid?

↓

Yes

↓

Business Logic

↓

Program Ends

------------------------

No

↓

Raise Exception

↓

Specific except

↓

Log Error

↓

Cleanup

↓

Program Ends

Memory Representation

Program

↓

Input

↓

Validation

↓

Exception?

↓

No

↓

Continue

------------------------

Yes

↓

Exception Object

↓

Specific Handler

↓

Logging

↓

finally

↓

Program Ends

Advantages

  • Improves reliability.
  • Simplifies debugging.
  • Prevents resource leaks.
  • Produces maintainable code.
  • Improves user experience.
  • Supports enterprise application development.

Best Practices Checklist

✅ Catch specific exceptions.

✅ Keep try blocks small.

✅ Validate input before processing.

✅ Use meaningful exception messages.

✅ Log unexpected exceptions.

✅ Release resources properly.

✅ Use with for file handling whenever possible.

✅ Create custom exceptions for business rules.


Common Anti-Patterns

❌ Using

except:

for every exception.


❌ Ignoring exceptions using

pass

❌ Raising generic exceptions.


❌ Writing large try blocks.


❌ Forgetting to close files.


Interview Questions

1. Why should specific exceptions be caught?

Answer

Specific exception handling makes programs easier to debug and prevents unrelated exceptions from being hidden.


2. Why are large try blocks discouraged?

Answer

Because they make it difficult to identify which statement caused the exception.


3. Why should exceptions be logged?

Answer

Logging preserves detailed error information for debugging and production monitoring.


4. Which is better for file handling?

Answer

The with statement because it automatically releases the resource.


5. Why should generic except: blocks be avoided?

Answer

Because they may hide unexpected programming errors.


6. Why should meaningful error messages be used?

Answer

They help users and developers understand the actual problem.


7. When should custom exceptions be created?

Answer

When business rules cannot be represented clearly using built-in exceptions.


Practice Programs

  1. Catch only ValueError.
  2. Log a ZeroDivisionError.
  3. Read a file using the with statement.
  4. Validate salary using a custom exception.
  5. Compare poor and good exception handling.

Quick Revision

  • Catch specific exceptions.
  • Keep try blocks small.
  • Validate inputs.
  • Use meaningful error messages.
  • Log important exceptions.
  • Use finally for cleanup.
  • Prefer with for file handling.
  • Use custom exceptions for business rules.

Chapter Summary

In this chapter, you learned:

  • Exception Handling Best Practices
  • Exception Logging
  • Resource Management
  • Using with Statement
  • Production-Ready Exception Handling
  • Practical Programs
  • Best Practices Checklist
  • Common Anti-Patterns
  • Interview Questions
  • Practice Programs
  • Quick Revision