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 Cursor

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.

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.
question mark

Which keyword pair is used to handle errors in Python?

Select the correct answer

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.

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.
question mark

Which keyword pair is used to handle errors in Python?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3
some-alt