PHP Basic Tutorial
MySQL Connection
PHP Advanced
An exception is an object that describes an error or an unexpected situation behaviour of a PHP script.
Exceptions are thrown by many PHP functions and classes.
User defined functions and classes can also throw exceptions.
Instead of stopping the script when an error occurs, you can catch and handle the error.
The throw
statement allows a user defined function or method to throw an exception. When an exception is thrown, the code following it will not be executed.
If an exception is not caught, a fatal error will occur with an "Uncaught Exception" message.
Lets try to throw an exception without catching it:
Try it yourself
The result will look something like this:
The try-catch
statement in PHP is used to handle exceptions that may occur during the execution of a block of code.
It allows you to catch exceptions, manage errors gracefully, and prevent the script from crashing unexpectedly.
try-catch
Statementtry { // Code that may throw an exception } catch (ExceptionType $e) { // Code to handle the exception }
Below example is show a message when an exception is thrown:
Try it yourself
The catch block indicates what type of exception should be caught and the name of the variable which can be used to access the exception. In the example above, the type of exception is Exception
and the variable name is $e
.
The try...catch...finally
statement can be used to catch exceptions.
This structure provides a way to handle exceptions and ensures that certain code always executes, regardless of whether an exception was thrown or not.
try...catch...finally
try { // Code that may throw an exception } catch (ExceptionType $e) { // Code to handle the exception } finally { // Code that will always run }
Below example show a message when an exception is thrown and then indicate that the process has ended:
Try it yourself
try
block checks if $num
is greater than 5. Since it is, it throws an exception with the message "Number is greater than 5"
.catch
block catches the exception and outputs the message "Caught exception: Number is greater than 5"
.finally
block will always execute and print "Finally block executed."
.Below example outputs a string even if an exception was not caught:
Try it yourself
In PHP, exceptions are objects of the built-in Exception
class (or subclasses of it). An exception object represents an error or an unexpected event that occurs during the execution of a script.
When an exception is thrown, an object of the Exception
class is created, which contains useful information about the error, such as the message, code, file, line number, and stack trace.
new Exception(message, code, previous)
Parameter |
Description |
---|---|
message |
Optional. A string describing why the exception was thrown |
code |
Optional. An integer that can be used to easily distinguish this exception from others of the same type |
previous |
Optional. If this exception was thrown in a catch block of another exception, it is recommended to pass that exception into this parameter |
PHP's Exception
object provides several useful methods that allow you to get detailed information about the exception:
Method |
Description |
---|---|
getMessage() |
Returns the exception message. |
getCode() |
Returns the exception code. |
getPrevious() |
Returns the previous exception if it exists. |
getFile() |
Returns the full path of the file in which the exception was thrown |
getLine() |
Returns the line number where the exception was thrown. |
Below example shows output information about an exception that was thrown:
Try it yourself