The source code to create a user-defined exception class is given below. The given program is compiled and executed successfully.
<?php
// PHP program to demonstrate the user-defined exception.
class UserException extends Exception
{
}
try
{
$exp = new UserException("User defined exception");
throw $exp;
}
catch(UserException $e)
{
printf("Exception: %s", $e->getMessage());
}
?>
Output:
Exception: User defined exception
Explanation:
Here, we created a class UserException by inheriting a predefined class Exception.
try
{
$exp = new UserException("User defined exception");
throw $exp;
}
Here, we created the object of UserException class and throw an exception using throw that will be caught by the catch block and print a specified message on the webpage.
Program/Source Code:
The source code to create a user-defined exception class is given below. The given program is compiled and executed successfully.
Output:
Explanation:
Here, we created a class UserException by inheriting a predefined class Exception.
Here, we created the object of UserException class and throw an exception using throw that will be caught by the catch block and print a specified message on the webpage.
need an explanation for this answer? contact us directly to get an explanation for this answer