CodeRef
CodeRef
  • Software
    • VSCode
  • Midjourney_AI
    • Midjourney Cheat Sheet
  • aws
    • S3 Reference
    • Services
  • bash
    • .bashrc File Contents
    • CAN
    • _System Config File Locations
    • argument-length-limits
    • Conditionals
    • Data Structures
    • File Permissions
    • File Syncing
    • File System
    • Functions
    • General
    • Loops
    • My Functions
    • Networking
    • Number Operations
    • OpenVPN
    • Operators
    • Resource Management
    • Serial RS232
    • Spinning Wheel Animation
    • SSH
    • Text Operations
    • Environment Variables
  • cpp
    • ChatGPT | Pointers vs. References
    • arrays
    • Classes
    • Data Types / Structures
    • Enumerated Classes
    • Exception Handling
    • Function Objects
    • Functions
    • I/O
    • Loops
    • Macros
    • Namespaces
    • New Features
    • Pointers
    • Scope
    • Smart Pointers
    • Raw String Literals
    • Style Guide
    • Switch Case
    • Templating
    • How to Use tinyxml2
    • Useful Libraries
    • google-test
    • Conditionals
    • Rule of Three/Five
    • Optional Parameters
    • Keywords
    • Filesystem
    • Random
    • Casting
    • tools
  • git
    • Code Review Dependency Strategy
    • Git Bisect Guide
    • Git Reference
    • removing-cherry-picks
    • Useful Tools
    • Graphite Reference
  • js
    • functions
    • Javascript Reference
  • linux
    • Display
    • Dual Boot with Windows
    • File System
    • NVIDIA
    • Sending/Receiving TCP/UDP Messages in Ubuntu
    • dynamically_linked_binaries
  • markdown
    • Images
    • obsidian-reference
  • python
    • Classes
    • Exceptions
    • Functions
    • Operations
    • Python Reference
    • unittest_command-line-args
    • unittest_magicmock_GPT
    • unittest_mock
    • unittest_printing
    • unittest_subtest
    • useful-stuff
    • jupyter
    • poetry
  • ros
    • _ROS Cheat Sheet
    • Create New Workspace
    • Install ROS
    • Node Sample - Listener
    • Node Sample - Talker
    • Node Template
    • Setup
    • urdf
  • excel
    • excel-reference
  • windows
    • File System
    • WSL - Windows Subsystem for Linux
    • WSL
  • software_engineering
    • uncle_bob_lectures
      • Overview
      • Lesson 01 - Notes
  • web
    • Front End
    • Hugo
    • new_frontend_tools
  • sql
    • cheatsheet
Powered by GitBook
On this page
  1. python

Exceptions

3 options:

  1. assert

  2. raise

  3. try/except block

Try/Except block:

If you handle exceptions within except blocks, the program will not end. The exception will be handled, and program execution will continue. Unless you put a raise statement in the except block.

Python will match exceptions in the order they're listed by type in the try/except block.

try:
    # Code that may raise an exception
    # ...
except ExceptionType1:
    # Code to handle ExceptionType1
    # ...
except ExceptionType2:
    # Code to handle ExceptionType2
    # ...
except ExceptionType3 as e:
    # How to catch an exception, add a message to it,
    # and throw it again.
    raise ExceptionType3(f"Message about {some_variable}") from e
except (ExceptionType4, ExceptionType5, ExceptionType6):
    # How to catch multiple exceptions and apply
    # the same logic for all of them.
except Exception as e:
    # Catches any exception type that is derived from the
    # `Exception` base class.
except:
    # Catch-all - handles any exception type.
    # It will handle any exceptions leftover after
    # other except blocks. Must go at the end,
    # otherwise all exception types would trigger
    # this block first.
    # ...
else:
    # Code to run if no exception occurs
    # ...
finally:
    # Code that always runs, regardless of whether an exception occurred or not
    # ...

Commonly used built-in exception types in Python:

  • Exception: The base class for all built-in exceptions.

  • 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 an invalid value.

  • NameError: Raised when a local or global name is not found.

  • IndexError: Raised when a sequence subscript is out of range.

  • KeyError: Raised when a dictionary key is not found.

  • FileNotFoundError: Raised when a file or directory is requested but cannot be found.

  • IOError: Raised when an I/O operation (such as reading or writing a file) fails.

  • ZeroDivisionError: Raised when a division or modulo operation is performed with a divisor of zero.

  • AttributeError: Raised when an attribute reference or assignment fails.

  • ImportError: Raised when an import statement fails to find a module.

  • NotImplementedError: Raised by abstract methods that should be overridden in subclasses.

  • OverflowError: Raised when the result of an arithmetic operation is too large to be expressed in the available numeric type.

  • SyntaxError: Raised when there is a syntax error in the code.

  • IndentationError: Raised when there is an incorrect indentation in the code.

From Bard:

Here's a guide to when to use assert, raise, and try in Python:

Assert:

  • Purpose: To verify internal program logic and assumptions during development and testing.

  • Behavior: If the assertion fails, an AssertionError is raised, typically halting execution.

  • Use cases:

    • Checking preconditions for functions and methods.

    • Validating data invariants within functions and classes.

    • Ensuring expected states during testing.

  • Example:

Python

def calculate_area(width, height):
    assert width > 0 and height > 0, "Dimensions must be positive"
    return width * height

Raise:

  • Purpose: To signal explicit errors or exceptional situations that require handling.

  • Behavior: Raises a specified exception, which can be caught using try-except blocks.

  • Use cases:

    • Custom errors for specific failure conditions.

    • Signaling errors from external sources (e.g., file I/O, network operations).

    • Re-raising caught exceptions for further handling.

  • Example:

Python

class InvalidInputError(Exception):
    pass

def divide(x, y):
    if y == 0:
        raise InvalidInputError("Cannot divide by zero")
    return x / y

Try:

  • Purpose: To handle potential exceptions and control program flow when errors occur.

  • Behavior: Executes code within the try block, catching specified exceptions in except blocks.

  • Use cases:

    • Preventing program crashes from unexpected errors.

    • Implementing alternative actions or error recovery.

    • Cleaning up resources (e.g., closing files) even when errors occur.

  • Example:

Python

try:
    with open("data.txt", "r") as file:
        data = file.read()
except FileNotFoundError:
    print("File not found")

Key Considerations:

  • Assertions: Primarily for debugging and testing, not for runtime error handling.

  • Raising Exceptions: Used to signal errors that need to be addressed.

  • Try-Except: Catches and handles exceptions for controlled error management.

Choose the appropriate construct based on your specific needs for debugging, error signaling, or error handling in different scenarios.

Last updated 10 months ago

Use code with caution.

Use code with caution.

Use code with caution.

Learn more
Learn more
Learn more