惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
小众软件
小众软件
D
Docker
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
V
V2EX
博客园 - 叶小钗
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Stack Overflow Blog
Stack Overflow Blog
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 【当耐特】
IT之家
IT之家
博客园 - 司徒正美
M
MIT News - Artificial intelligence
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
罗磊的独立博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog

Stonecharioteer on Tech

I Traced My Traffic Through a Home Tailscale Exit Node What Was I Reading Last? In Three Not-So-Easy Pieces Dogfooding Is Hard Code blocks in your books, finally GoForGo v0.9.0 Merrilin - We built an app to read books I use a Macbook now Data Structures & Algorithms - Preparing for Interviews Using a local DNS namespace for local service discovery Direction KOllector - Publishing KOReader Highlights gbt: branches touched in the last 24 hours A Soiree into Symbols in Ruby Some Smalltalk about Ruby Loops Ruby Blocks Returning from Ruby Blocks, Procs and Lambdas My Linux Laptop Finally Works: How Claude Helped Me Fix Years of Annoyances TIL: Watchexec - Modern File Watching for Development Workflows A Less Busy Mind GoForGo - Learn Go through live examples Migrating My Old Blog to Hugo with Claude The Qtile Window Manager: A Python-Powered Tiling Experience Read the RFCs that Built the Internet Py-x-Protobuf - Or How I Learned to Stop Worrying and Love Protocol Buffers Python Reverse a List New Beginnings Leaving ChainSafe Systems Screen Lock for Cinnamon Desktop using Zenity and Terminal Commands Crews Not Teams A System for Getting Better at LeetCode
TIL: Python Raise Statement and Traceback Preservation
2020-08-07 · via Stonecharioteer on Tech

Python Exception Handling

Python’s Raise Statement with From Clause

Exception Chaining Syntax

Basic Exception Chaining

1
2
3
4
5
6
try:
    # Some operation that might fail
    risky_operation()
except SomeException as e:
    # Re-raise with context preserved
    raise NewException("Custom message") from e

Benefits of Exception Chaining

  • Full Traceback Preservation: Maintains complete error history
  • Better Debugging: Shows both original and current exception contexts
  • Clear Error Propagation: Makes it obvious how errors propagated through code
  • Professional Error Handling: Industry best practice for exception management

Exception Handling Best Practices

When to Use Exception Chaining

  • Converting between exception types (e.g., library exceptions to domain exceptions)
  • Adding context to low-level errors
  • Creating more meaningful error messages for users
  • Maintaining debugging information in complex call stacks

Exception Suppression

1
2
# Suppress original exception (use sparingly)
raise NewException("Message") from None

Debugging Benefits

  • Helps identify root cause of complex errors
  • Provides complete context for error investigation
  • Essential for production debugging and error monitoring
  • Improves error reporting and logging quality