0
0
Pythonprogramming~10 mins

Extending built-in exceptions in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Extending built-in exceptions
Define new Exception class
Inherit from built-in Exception
Add custom __init__ or methods
Raise new Exception
Catch with try-except
Handle or print custom message
You create a new error type by making a class that inherits from a built-in exception, then raise and catch it like normal.
Execution Sample
Python
class MyError(Exception):
    def __init__(self, message):
        super().__init__(message)

try:
    raise MyError("Oops!")
except MyError as e:
    print(e)
This code defines a new error type MyError, raises it with a message, and catches it to print the message.
Execution Table
StepActionEvaluationResult
1Define class MyError inheriting ExceptionClass createdMyError ready to use
2Enter try blockNo error yetProceed
3Raise MyError with message 'Oops!'Exception raisedJump to except block
4Catch MyError as ee holds MyError instanceReady to handle
5Print(e)Calls __str__ on MyError instanceOutput: Oops!
💡 Exception handled, program continues normally after except block
Variable Tracker
VariableStartAfter Step 3After Step 4Final
eundefinedMyError('Oops!')MyError('Oops!')MyError('Oops!')
Key Moments - 3 Insights
Why do we inherit from Exception when creating MyError?
Inheriting from Exception makes MyError behave like a normal error, so it can be raised and caught properly (see Step 1 and Step 3 in execution_table).
What happens when we raise MyError inside try?
Raising MyError stops normal flow and jumps to the except block that matches MyError (Step 3 and Step 4).
How does print(e) show 'Oops!'?
The message is stored in the Exception base class and __str__ returns it, so print(e) outputs the message passed when raising (Step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'e' after Step 3?
AMyError('Oops!')
Bundefined
CException('Oops!')
DNone
💡 Hint
Check variable_tracker row for 'e' after Step 3
At which step does the program jump to the except block?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
See execution_table Action and Result columns at Step 3
If we did not inherit from Exception, what would happen when raising MyError?
AIt would raise normally and be caught
BIt would cause a TypeError
CIt would not be recognized as an exception
DIt would print 'Oops!' anyway
💡 Hint
Refer to key_moments about why inheritance from Exception is needed
Concept Snapshot
class MyError(Exception):
    def __init__(self, message):
        super().__init__(message)

Raise with: raise MyError('msg')
Catch with: except MyError as e:
Use inheritance to behave like built-in exceptions.
Full Transcript
This example shows how to create a new error type by making a class that inherits from Exception. When we raise this new error inside a try block, the program jumps to the matching except block. The error message is stored and printed when we print the caught exception. This helps us make custom errors with special messages or behavior while keeping normal error handling.