PHP Exceptions
What is an Exception?
An exception is an object that explains a PHP script's error or unusual behavior.
Many PHP functions and classes will throw exceptions.
Exceptions can also be thrown by user-defined functions and classes.
When a function encounters data it can't utilize, it can use exceptions to stop it.
Throwing an Exception
The throw
declaration allows an exception to a user-defined function or way. The following code will not be performed if an exception is cast.
If an exception is not captured, a fatal error will be thrown with the message "Uncaught Exception".
Example :- Let's see whether we can throw an exception without being caught :
Output :-
Stack trace: #0 C:\webfolder\test.php(9):
divide(5, 0) #1 {main} thrown in C:\webfolder\test.php on line 4
The try...catch Statement
In order to prevent the above-mentioned problem, we can utilize the try...catch
statement to capture exceptions and proceed.
Syntax :-
try {
code that can throw exceptions
} catch(Exception $e) {
code that runs when an exception is caught
}
Example :- If an exception is thrown, display a message :
Output :-
The catch
block shows what type of exception and the name of the variable used to obtain the exception should be collected. The exception type is Exception and the variable name is $e in the following example.
Related Links
The try...catch...finally Statement
To capture exceptions, use the try...catch...finally
phrase. Regardless of whether an exception was caught, code in the finally
block will always be executed. If finally
is present, the catch
block is not required.
Syntax :-
try {
code that can throw exceptions
} catch(Exception $e) {
code that runs when an exception is caught
} finally {
code that always runs regardless of whether an exception was caught
}
Example 1 :- When an exception is thrown, display a notice and then mark the process as complete :
Output :-
Example 2 :- Even if an exception isn't thrown, print a string :
Output :-
The Exception Object
The object Exception contains information about the error or unexpected action.
Syntax :-
new Exception(message, code, previous)
Parameter Values :-
Parameter | Description |
---|---|
format | Optional. A string describes the reason for this exception was thrown. |
code | Optional. This integer can be used to differentiate this exception from others of the same type. |
prevoius | Optional. It is suggested to pass this exception into this argument if it was thrown in a catch block of another exception. |
Methods :-
The table below displays some of the ways that can be used to retrieve information about an exception when it is caught :
Method | Description |
---|---|
getMessage() | Returns a string that shows why the exception has been thrown |
getPrevious() | This function returns the previous exception if this exception has been generated by another. If not, it will be null again. |
getCode() | The exception code is returned. |
getFile() | The entire path to the file where the exception was thrown is returned. |
getLine() | The line number of the code that threw the exception is returned. |
Example :- Give details about a thrown exception :
getCode();
$message = $ex->getMessage();
$file = $ex->getFile();
$line = $ex->getLine();
echo "Exception thrown in $file on line $line: [Code $code]
$message";
}
?>
Output :-
Related Links