PHP 5 introduced new object oriented way of dealing with errors. Exceptions are used to change the normal flow of a script if a specified error occurs. Exception handling is used to change the normal flow of the code execution if a specified error (exceptional) condition occurs. This condition is called an exception.

When an exception is triggered:

  • Current code state is saved
  • Code execution will switch to a predefined (custom) exception handler function
  • Depending on the situation, the handler may then resume the execution from the saved code state, terminate the script execution or continue the script from a different location in the code

When an exception is thrown, the code following it is not executed. PHP tries to find the matching "catch" block. If an exception is not caught, a fatal error is issued with an "Uncaught Exception" message. To avoid the error, you need to create the proper code to handle an exception.

Proper exception code includes:

Try: A function using an exception should be in a "try" block. If the exception does not trigger, the code continues as normal. If the exception triggers, an exception is "thrown".

Throw: This is how you trigger an exception. Each "throw" must have at least one "catch".

Catch: A "catch" block retrieves an exception and creates an object containing the exception information.

//create function with an exception
function checkNum($number) {
if($number>1) {
throw new Exception("Value must be 1 or below");
return true;

//trigger exception in a "try" block
try {
//If the exception is thrown, this text will not be shown
echo 'If you see this, the number is 1 or below';

//catch exception
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();

The code above throws an exception and catches it. The checkNum() function checks if a number is greater than 1. If it is, an exception is thrown. The checkNum() function is called in a "try" block. The "catch" block retrieves the exception and creates an object ($e) containing the exception information. The error message from the exception is echoed by calling $e->getMessage() from the exception object.

Following functions can be used from Exception class.

  • getMessage(): message of exception
  • getCode(): code of exception
  • getFile(): source filename
  • getLine(): source line
  • getTrace(): n array of the backtrace()
  • getTraceAsString(): formated string of trace

Syntax For Throwing Exception

throw new Exception("This is an exception error");

The keyword used to throw the exception is throw. The new Exception() creates an exception object and passes "This is an exception error" string as the message parameter.

Syntax For Try and Catch

The "try" code block is the code to be executed that could potentially raise an exception. The "catch (Exception $e)" code block catches the thrown exception and assigns the exception object to the variable $e.

try {
//code goes here that could potentially throw an exception
catch (Exception $e) {
//exception handling code goes here

Difference Between Errors and Exception

Exceptions are thrown and intended to be caught while errors are generally irrecoverable. Exceptions are handled in an object oriented way. When an exception is thrown; an exception object is created that contains the exception details.