Fiveable

🧵Programming Languages and Techniques I Unit 12 Review

QR code for Programming Languages and Techniques I practice questions

12.3 Try-Catch Blocks

12.3 Try-Catch Blocks

Written by the Fiveable Content Team • Last updated August 2025
Written by the Fiveable Content Team • Last updated August 2025
🧵Programming Languages and Techniques I
Unit & Topic Study Guides

Exception handling is a crucial programming concept that helps manage errors gracefully. Try-catch blocks allow developers to anticipate and handle potential issues, preventing crashes and improving code reliability. This technique is essential for robust software development.

Python's try-except syntax offers a flexible approach to exception handling. With multiple except clauses, custom exception types, and the ability to reraise exceptions, developers can create sophisticated error management systems. Try-with-resources further enhances resource management, ensuring proper cleanup.

Exception Handling with Try-Catch Blocks

Exception handling with try-except

  • Try-except blocks catch and handle exceptions preventing program crashes and gracefully managing errors
  • Try block contains code that may raise an exception while except block handles the exception if it occurs
  • Python syntax: try: [code that may raise exception] except ExceptionType: [code to handle exception]
  • Common built-in exception types include ValueError, TypeError, ZeroDivisionError, and FileNotFoundError
  • Generic except clause catches all exceptions: except: [handle any exception]
Exception handling with try-except, 例外処理 | Python で例外処理を制御する方法

Multiple except clauses

  • Multiple except clauses handle different types of exceptions separately
  • Syntax: try: [code] except ExceptionType1: [handle] except ExceptionType2: [handle]
  • Order matters more specific exceptions first, generic except clause last
  • Exception hierarchies parent exceptions catch child exceptions
  • Access exception information using 'as' keyword (except ValueError as e:) extracting error messages and details
Exception handling with try-except, CS105: Example: Try and Except | Saylor Academy

Reraising exceptions

  • Reraising exceptions allows partial handling and propagates errors to higher-level handlers
  • Syntax: raise, raise ExceptionType, or raise ExceptionType(message)
  • Chain exceptions using 'from' keyword: raise NewException from OriginalException
  • Preserve original traceback when reraising for comprehensive error information

Try-with-resources for resource management

  • Try-with-resources automatically closes resources (files, database connections) ensuring proper cleanup
  • Python's context manager protocol uses enter and exit methods
  • 'with' statement syntax: with resource as variable:
  • Multiple resources in single 'with' statement use comma-separated list
  • Create custom context managers using contextlib module or implementing enter and exit methods
  • Benefits include automatic resource cleanup and reduced potential for resource leaks
Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly → and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot

2,589 studying →