0
0
PHPprogramming~10 mins

Custom exception classes in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom exception classes
Start
Define custom exception class
Try block: code runs
Exception thrown?
NoContinue normal flow
Yes
Catch block: catch custom exception
Handle exception
End
This flow shows how a custom exception class is defined, then used in a try block, caught, and handled.
Execution Sample
PHP
<?php
class MyException extends Exception {}

try {
  throw new MyException("Oops!");
} catch (MyException $e) {
  echo $e->getMessage();
}
This code defines a custom exception, throws it, catches it, and prints its message.
Execution Table
StepActionEvaluationResult
1Define class MyException extending ExceptionClass createdMyException ready
2Enter try blockNo exception yetRunning code inside try
3Throw new MyException with message 'Oops!'Exception thrownMyException object created
4Catch block matches MyExceptionCatch block enteredException caught
5Call getMessage() on exceptionReturns 'Oops!'Output: Oops!
6End of scriptNo more codeScript ends
💡 Exception thrown and caught, script ends normally
Variable Tracker
VariableStartAfter Step 3After Step 4Final
$eundefinedMyException object with message 'Oops!'Same object in catchSame object
Key Moments - 3 Insights
Why do we extend Exception to create a custom exception?
Extending Exception lets us create a new type of error that can be caught separately, as shown in step 1 and step 4 of the execution_table.
What happens if we throw an exception but don't catch it?
If not caught, the script stops with an error. Here, step 4 shows catching prevents that and allows handling.
How do we get the message from the exception?
We call getMessage() on the exception object, as shown in step 5 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at step 5?
AOops!
BMyException
CNo output
DError message
💡 Hint
Check the 'Result' column at step 5 in execution_table
At which step does the exception get caught?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for 'Catch block entered' in the 'Evaluation' column
If we remove the catch block, what happens to the script?
AIt runs normally without output
BIt throws an uncaught exception error
CIt prints 'Oops!' automatically
DIt ignores the exception
💡 Hint
Refer to key_moments about what happens if exception is not caught
Concept Snapshot
Custom exception classes in PHP:
- Define by extending Exception class
- Use try block to run code
- Throw custom exception with throw
- Catch with catch (CustomException $e)
- Access message with getMessage()
Full Transcript
This example shows how to create a custom exception class in PHP by extending the built-in Exception class. The code runs inside a try block where it throws the custom exception. The catch block catches this specific exception type and handles it by printing the message. The execution table traces each step: defining the class, entering try, throwing exception, catching it, printing message, and ending the script. Variables track the exception object as it is created and caught. Key moments clarify why extending Exception is needed, what happens if exceptions are not caught, and how to get the message. The quiz tests understanding of output, catching step, and behavior without catch. The snapshot summarizes the syntax and flow for quick reference.