Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Exceptions & Debugging | Control Flow & Logic
Introduction to Python with AI

bookExceptions & Debugging

When coding, errors are inevitable. Some come from logic mistakes, others appear during execution β€” like dividing by zero, opening a missing file, or converting invalid input.

These runtime errors are called exceptions. Python handles them with try and except blocks, allowing the program to recover or show a clear message instead of crashing.

Note
Example Prompts
  • What is an exception in Python? Give code example where an error stops the program.
  • Give code example that includes a try block with a risky operation and an except block that handles the error.
  • Give code example that uses a try block followed by multiple except blocks that catch different error types. Do not include else or finally.
  • Give code example that uses all four parts of the structure: try, except, else, and finally.
  • How can print statements help in debugging Python code? Give code example.

What Is an Exception?

An exception is Python's signal that something unexpected happened. It stops normal execution and shows a traceback unless handled.

Common examples:

  • ZeroDivisionError: divide by zero;
  • ValueError: invalid value, like converting "abc" to int;
  • TypeError: incompatible types, e.g., number + string;
  • FileNotFoundError: opening a missing file;
  • IndexError: list index out of range;
  • KeyError: missing dictionary key.

Exceptions are built-in classes and can be caught with except.

Handling Exceptions with Try and Except

Wrap risky code in a try block to prevent crashes. If an error occurs, Python moves to the except block.

There you can show a message, log details, or take alternative action.

Multiple Except Blocks and General Catching

You can handle specific errors with separate except blocks, or use a general except to catch anything unexpected.

Multiple exception types can also be grouped in one block using parentheses.

The Else and Finally Clauses

Python's error handling can also include else and finally:

  • else runs only if no exception occurred;
  • finally always runs β€” even if an exception happened.

finally is often used to close files or release resources.

Debugging with Print

Debugging helps find what went wrong. A simple method is adding print() statements to trace variable values and program flow.

This shows where errors occur and helps narrow down issues. Later, you can use advanced debuggers, but print is always a useful first step.

Summary

  • Exceptions are runtime errors like division by zero or missing files;
  • You can handle them using try and except blocks to avoid crashes;
  • Use specific exception types when possible, and finally to clean up resources.
  • print() is your first and fastest debugging tool.

Try It Yourself

  1. Try writing a small program that causes and handles a TypeError;
  2. Start with this: a = "4" and b = 5, then try print(a + b);
  3. Wrap it in a try-except block and print a message like: "Cannot add string and integer!" when it fails.
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 5

bookExceptions & Debugging

Swipe to show menu

When coding, errors are inevitable. Some come from logic mistakes, others appear during execution β€” like dividing by zero, opening a missing file, or converting invalid input.

These runtime errors are called exceptions. Python handles them with try and except blocks, allowing the program to recover or show a clear message instead of crashing.

Note
Example Prompts
  • What is an exception in Python? Give code example where an error stops the program.
  • Give code example that includes a try block with a risky operation and an except block that handles the error.
  • Give code example that uses a try block followed by multiple except blocks that catch different error types. Do not include else or finally.
  • Give code example that uses all four parts of the structure: try, except, else, and finally.
  • How can print statements help in debugging Python code? Give code example.

What Is an Exception?

An exception is Python's signal that something unexpected happened. It stops normal execution and shows a traceback unless handled.

Common examples:

  • ZeroDivisionError: divide by zero;
  • ValueError: invalid value, like converting "abc" to int;
  • TypeError: incompatible types, e.g., number + string;
  • FileNotFoundError: opening a missing file;
  • IndexError: list index out of range;
  • KeyError: missing dictionary key.

Exceptions are built-in classes and can be caught with except.

Handling Exceptions with Try and Except

Wrap risky code in a try block to prevent crashes. If an error occurs, Python moves to the except block.

There you can show a message, log details, or take alternative action.

Multiple Except Blocks and General Catching

You can handle specific errors with separate except blocks, or use a general except to catch anything unexpected.

Multiple exception types can also be grouped in one block using parentheses.

The Else and Finally Clauses

Python's error handling can also include else and finally:

  • else runs only if no exception occurred;
  • finally always runs β€” even if an exception happened.

finally is often used to close files or release resources.

Debugging with Print

Debugging helps find what went wrong. A simple method is adding print() statements to trace variable values and program flow.

This shows where errors occur and helps narrow down issues. Later, you can use advanced debuggers, but print is always a useful first step.

Summary

  • Exceptions are runtime errors like division by zero or missing files;
  • You can handle them using try and except blocks to avoid crashes;
  • Use specific exception types when possible, and finally to clean up resources.
  • print() is your first and fastest debugging tool.

Try It Yourself

  1. Try writing a small program that causes and handles a TypeError;
  2. Start with this: a = "4" and b = 5, then try print(a + b);
  3. Wrap it in a try-except block and print a message like: "Cannot add string and integer!" when it fails.
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3
some-alt