Title: Custom Exceptions in Python — Why Your Tracebacks Vanish
Open Graph Title: Custom Exceptions in Python — Why Your Tracebacks Vanish
Description: Missing 'from' in raise statements silently kills tracebacks. See the exception chaining pattern that preserves root cause in production logs.
Open Graph Description: Missing 'from' in raise statements silently kills tracebacks. See the exception chaining pattern that preserves root cause in production logs.
Opengraph URL: https://thecodeforge.io/python/custom-exceptions-python/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/python/custom-exceptions-python/"
},
"headline": "Custom Exceptions in Python — Why Your Tracebacks Vanish",
"description": "Missing 'from' in raise statements silently kills tracebacks. See the exception chaining pattern that preserves root cause in production logs.",
"keywords": "Custom Exceptions in Python — Why Your Tracebacks Vanish, Python, Intermediate, Exception Handling, Missing 'from' in raise statements silently kills tracebacks. See the exception chaining pattern that preserves root cause in production logs.",
"proficiencyLevel": "Intermediate",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-03-05T00:00:00+00:00",
"dateModified": "2026-10-31T00:00:00+00:00",
"url": "https://thecodeforge.io/python/custom-exceptions-python/",
"inLanguage": "en",
"educationalLevel": "Intermediate",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "Python", "item": "https://thecodeforge.io/python/"},
{"@type": "ListItem", "position": 3, "name": "Custom Exceptions in Python — Why Your Tracebacks Vanish", "item": "https://thecodeforge.io/python/custom-exceptions-python/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "Should I always create a custom exception or can I just reuse ValueError and TypeError?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Reuse built-in exceptions when the semantics genuinely match and you're writing utility/library code — subclassing ValueError for invalid input is totally fine and lets callers use familiar catch patterns. Create custom exceptions when you're modelling domain concepts (payments, inventory, authorisation) that have no natural built-in equivalent, or when you need to carry structured data beyond a message string."
}
},
{
"@type": "Question",
"name": "What is the minimum code needed for a useful custom exception in Python?",
"acceptedAnswer": {
"@type": "Answer",
"text": "At its simplest: a class that inherits from Exception with a docstring explaining when it's raised. That's literally 'class MyError(Exception): pass' plus a docstring. You only need to override __init__ when you want to attach structured attributes. The docstring alone makes it dramatically more useful than a bare ValueError because the type name is self-documenting."
}
},
{
"@type": "Question",
"name": "How do I access the original exception when using raise...from?",
"acceptedAnswer": {
"@type": "Answer",
"text": "The original exception is stored on the __cause__ attribute of the new exception. So if you write 'raise DomainError() from db_error', you can later access caught_error.__cause__ to get the original db_error object, including its type and message. If Python implicitly chained exceptions (without from), the original is on __context__ instead."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large