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.

You can also search for these topics, exceptions in php, php exception types, php exception class, what is an exception using php, working of php exceptions.

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 :

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero");
  }
  return $dividend / $divisor;
}

echo divide(5, 0);
?>

Output :-

Fatal error: Uncaught Exception: Division by zero in C:\webfolder\test.php:4
Stack trace: #0 C:\webfolder\test.php(9):
divide(5, 0) #1 {main} thrown in C:\webfolder\test.php on line 4

You can also search for these topics, throwing an exception in php, php throwing an exception or not, php exception throwing in php, what is uncaught exception in php, Example for Throwing an Exception using php, exception is thrown using php.

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 :

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero");
  }
  return $dividend / $divisor;
}

try {
  echo divide(5, 0);
} catch(Exception $e) {
  echo "Unable to divide.";
}
?>

Output :-

Unable to divide.

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.

You can also search for these topics, php try catch Statement, php try catch syntax, php try catch prepared statement, Example for php try catch statement, working of try catch statement in php.

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 :

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero");
  }
  return $dividend / $divisor;
}

try {
  echo divide(5, 0);
} catch(Exception $e) {
  echo "Unable to divide. ";
} finally {
  echo "Process complete.";
}
?>

Output :-

Unable to divide. Process complete.

Example 2 :- Even if an exception isn't thrown, print a string :

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero");
  }
  return $dividend / $divisor;
}

try {
  echo divide(5, 0);
} finally {
  echo 'Process complete.';
}
?>

Output :-

Process complete.

You can also search for these topics, php try catch finally Statement, php try catch finally syntax, php try catch finally prepared statement, Example for php try catch finally statement, working of try catch finally statement in php.

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 :

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero", 1);
  }
  return $dividend / $divisor;
}

try {
  echo divide(5, 0);
} catch(Exception $ex) {
  $code = $ex->getCode();
  $message = $ex->getMessage();
  $file = $ex->getFile();
  $line = $ex->getLine();
  echo "Exception thrown in $file on line $line: [Code $code]
  $message";
}
?>

Output :-

Exception thrown in /home/TwT0ln/prog.php on line 8: [Code 1] Division by zero

You can also search for these topics, php exception object, php exception object syntax, php create exception object, php get exception object, exception object in php, Example for exception object, php exception object parameters, php exception object methods.