Q:

PHP program to create a user-defined exception class

0

Here, we will create a user-defined class that inherits the Exception class and then we will throw an exception using the throw keyword.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now