[2026] Python Exception Handling | try-except, else, finally, raise, Custom Exceptions

[2026] Python Exception Handling | try-except, else, finally, raise, Custom Exceptions

이 글의 핵심

Learn Python exceptions: try-except-else-finally, raising errors, custom exception classes, and safe patterns for files, I/O, and retries—with examples.

Introduction

“Handling errors gracefully”

Exception handling is central to building stable, maintainable Python programs.

1. Basic exception handling

try-except

다음은 python를 활용한 상세한 구현 코드입니다. 에러 처리를 통해 안정성을 확보합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

# Basic form
try:
    result = 10 / 0
except ZeroDivisionError:
    print("Cannot divide by zero")
    result = None
# Multiple exception types
try:
    number = int(input("Enter a number: "))
    result = 10 / number
except ValueError:
    print("Please enter a valid number")
except ZeroDivisionError:
    print("Enter a non-zero number")
# Bind the exception object
try:
    file = open('missing.txt', 'r')
except FileNotFoundError as e:
    print(f"Error: {e}")

2. try-except-else-finally

Full structure

다음은 python를 활용한 상세한 구현 코드입니다. 에러 처리를 통해 안정성을 확보합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

try:
    # Code that might fail
    file = open('data.txt', 'r')
    content = file.read()
except FileNotFoundError:
    # Runs when an exception occurs
    print("File not found")
else:
    # Runs only if no exception in try
    print(f"Read OK: {len(content)} characters")
finally:
    # Always runs (cleanup)
    if 'file' in locals():
        file.close()
    print("Done")

3. Raising exceptions (raise)

Basic raise

아래 코드는 python를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다, 에러 처리를 통해 안정성을 확보합니다. 코드를 직접 실행해보면서 동작을 확인해보세요.

def divide(a, b):
    if b == 0:
        raise ValueError("b cannot be zero")
    return a / b
try:
    result = divide(10, 0)
except ValueError as e:
    print(f"Error: {e}")

Re-raising

아래 코드는 python를 사용한 구현 예제입니다. 함수를 통해 로직을 구현합니다, 에러 처리를 통해 안정성을 확보합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

def process_data(data):
    try:
        result = int(data)
    except ValueError:
        print("Conversion failed")
        raise  # propagate to caller
try:
    process_data("abc")
except ValueError:
    print("Handled at outer level")

4. Custom exceptions

User-defined exceptions

다음은 python를 활용한 상세한 구현 코드입니다. 클래스를 정의하여 데이터와 기능을 캡슐화하며, 에러 처리를 통해 안정성을 확보합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

class InsufficientBalanceError(Exception):
    """Raised when withdrawal exceeds balance."""
    def __init__(self, balance, amount):
        self.balance = balance
        self.amount = amount
        super().__init__(f"Insufficient balance: {balance} (needed: {amount})")
class BankAccount:
    def __init__(self, owner, balance):
        self.owner = owner
        self.balance = balance
    
    def withdraw(self, amount):
        if amount > self.balance:
            raise InsufficientBalanceError(self.balance, amount)
        self.balance -= amount
        return self.balance
# Usage
account = BankAccount("Alice", 10000)
try:
    account.withdraw(15000)
except InsufficientBalanceError as e:
    print(e)  # Insufficient balance: 10000 (needed: 15000)
    print(f"Current balance: {e.balance}")

5. Common built-in exceptions

Frequently used types

다음은 python를 활용한 상세한 구현 코드입니다. 에러 처리를 통해 안정성을 확보합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

# ValueError: wrong value
try:
    int("abc")
except ValueError:
    print("Conversion failed")
# TypeError: wrong types
try:
    "hello" + 5
except TypeError:
    print("Type mismatch")
# KeyError: missing dict key
try:
    data = {'name': 'Alice'}
    print(data['age'])
except KeyError:
    print("Key missing")
# IndexError: index out of range
try:
    arr = [1, 2, 3]
    print(arr[10])
except IndexError:
    print("Index out of range")
# FileNotFoundError: missing file
try:
    open('missing.txt', 'r')
except FileNotFoundError:
    print("File not found")

6. Practical examples

Safe JSON file reading

다음은 python를 활용한 상세한 구현 코드입니다. 필요한 모듈을 import하고, 함수를 통해 로직을 구현합니다, 에러 처리를 통해 안정성을 확보합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

import json
def safe_read_json(filename):
    """Read a JSON file safely."""
    try:
        with open(filename, 'r', encoding='utf-8') as f:
            return json.load(f)
    except FileNotFoundError:
        print(f"File not found: {filename}")
        return {}
    except json.JSONDecodeError as e:
        print(f"JSON parse error: {e}")
        return {}
    except Exception as e:
        print(f"Unexpected error: {e}")
        return {}

Retry logic

다음은 python를 활용한 상세한 구현 코드입니다. 필요한 모듈을 import하고, 함수를 통해 로직을 구현합니다, 에러 처리를 통해 안정성을 확보합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

import time
def retry_operation(func, max_attempts=3):
    """Retry func until success or attempts exhausted."""
    for attempt in range(max_attempts):
        try:
            return func()
        except Exception as e:
            print(f"Attempt {attempt + 1} failed: {e}")
            if attempt < max_attempts - 1:
                time.sleep(1)
            else:
                raise
# Usage
def unstable_operation():
    import random
    if random.random() < 0.7:
        raise ConnectionError("connection failed")
    return "success"
try:
    result = retry_operation(unstable_operation)
    print(result)
except Exception as e:
    print(f"Final failure: {e}")

Practical tips

Exception-handling best practices

다음은 python를 활용한 상세한 구현 코드입니다. 에러 처리를 통해 안정성을 확보합니다. 각 부분의 역할을 이해하면서 코드를 살펴보시기 바랍니다.

# ✅ Catch specific exceptions
try:
    value = int(user_input)
except ValueError:
    print("Please enter a number")
# ❌ Bare except Exception (harder to debug)
try:
    value = int(user_input)
except Exception:
    print("Something went wrong")
# ✅ Use the exception message
try:
    file = open('data.txt', 'r')
except FileNotFoundError as e:
    print(f"File error: {e}")
# ✅ Prefer context managers over manual close in finally
try:
    with open('data.txt', 'r') as file:
        pass
finally:
    pass  # file already closed by with

Summary

Key takeaways

  1. try-except: handle expected failures.
  2. finally: always runs—use for cleanup when not using with.
  3. raise: signal errors; bare raise re-raises the current exception.
  4. Custom exceptions: subclass Exception and store context on self.
  5. Best practice: catch narrow types; avoid swallowing bugs with overly broad handlers.

Next steps


... 996 lines not shown ... Token usage: 63706/1000000; 936294 remaining Start-Sleep -Seconds 3