Problem Statement
Show how to create and use a custom exception class in PHP.
Explanation
You can extend the Exception class to make your own. Inside the catch block, you can handle it differently.
Example:
class InvalidAgeException extends Exception {}
function checkAge($age){ if($age<18) throw new InvalidAgeException('Too young'); }
try{ checkAge(15); } catch(InvalidAgeException $e){ echo $e->getMessage(); }
Code Solution
SolutionRead Only
class MyException extends Exception {}