0
0
Pythonprogramming~10 mins

Creating exception classes in Python - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating exception classes
Define new Exception class
Raise the new Exception
Catch the Exception with try-except
Handle or print error message
End
You first create a new exception class, then raise it in code, catch it using try-except, and finally handle or display the error.
Execution Sample
Python
class MyError(Exception):
    pass

try:
    raise MyError("Oops!")
except MyError as e:
    print(e)
This code defines a new exception class, raises it, catches it, and prints the error message.
Execution Table
StepActionEvaluationResult
1Define class MyError(Exception)Class createdMyError is ready to use
2Enter try blockNo error yetProceed
3Execute raise MyError("Oops!")Raise exceptionException MyError("Oops!") raised
4Exception caught by except MyError as eCatch exceptionVariable e holds MyError("Oops!")
5Execute print(e)Print error messageOutput: Oops!
6End of try-except blockNo more codeProgram ends normally
💡 Exception raised and caught, program ends after handling
Variable Tracker
VariableStartAfter Step 3After Step 4Final
eundefinedundefinedMyError('Oops!')MyError('Oops!')
Key Moments - 3 Insights
Why do we inherit from Exception when creating MyError?
Inheriting from Exception makes MyError a proper error type that can be raised and caught, as shown in step 1 and step 3 of the execution_table.
What happens if we raise MyError but do not catch it?
If not caught, the program stops with an error message. Here, step 4 shows catching the error to prevent a crash.
Why do we use 'as e' in except MyError as e?
'as e' stores the caught exception in variable e, so we can access its message, as seen in step 4 and step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of variable 'e' after step 4?
ANone
Bundefined
CMyError('Oops!')
DException
💡 Hint
Check the variable_tracker row for 'e' after step 4.
At which step is the exception actually raised?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column in execution_table where 'raise' is mentioned.
If we remove the except block, what happens after step 3?
AProgram crashes with error
BProgram continues normally
CException is ignored
DException is caught automatically
💡 Hint
Refer to key_moments about what happens if exception is not caught.
Concept Snapshot
Create a new exception by defining a class inheriting Exception.
Raise it with 'raise MyError("message")'.
Catch it using try-except: 'except MyError as e:'.
Use 'e' to access the error message.
This helps handle errors clearly and safely.
Full Transcript
We start by defining a new exception class called MyError that inherits from Exception. This makes it a proper error type. Then, inside a try block, we raise this error with a message 'Oops!'. The program jumps to the except block that catches MyError and stores it in variable e. We print e, which shows the message. Finally, the program ends normally after handling the error. This process helps us create and manage custom errors in Python.