PHP Exceptions


What is an Exception?

The exception is the one that describes the error or unexpected behavior of the PHP script.

The exception is thrown in many PHP tasks and classes.

User-defined tasks and classes can also do differently.

The exception is a good way to stop work when it comes to data that it can use.


Throwing an Exception

The throw statement allows for a defined function or method to do otherwise. If alternatives are thrown, the following code will not be used.

If the exception is not detected, a dangerous error will occur with the message "Uncaught Exception".

Let's try something different without catching you:


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

echo divide(5, 0);
?>

The result will look like this:

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

The try...catch Statement

To avoid the mistake from the example above, we can use the try...catch statement to catch the opposite and continue the process.

Syntax
try {
  code that can throw exceptions
} catch(Exception $e) {
  code that runs when an exception is caught
}

Example

Show a message when an exception is thrown:

<?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.";
}
?>

The catch block indicates which type of variation to hold and the name of the variable that can be used for different access. In the example above, the variant is Exception and the variable name is $ e.


The try...catch...finally Statement

Try...catch...finally the statement can be used for a different catch. The code in the finally of the block will always work regardless of whether the exception is detected. If finally is available, a catch block can be selected.

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

Show a message when an exception is thrown and then indicate that the process has ended:

<?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.";
}
?>


Example

Output a string even if an exception was not caught:

<?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.";
}
?>


The Exception Object

The Exception Object contains information about an unexpected error or behavior experienced by an employee.

Syntax
new Exception(message, code, previous)

Parameter Values
Parameter Description
message Optional. A string describing why the exception was thrown
code Optional. An integer that can be used 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
Methods

If you are holding a different one, the following table shows some of the ways in which you can get information about the difference:

Method Description
getMessage() Returns a string describing why the exception was thrown
getPrevious() If this exception was triggered by another one, this method returns the previous exception. If not, then it returns null
getCode() Returns the exception code
getFile() Returns the full path of the file in which the exception was thrown
getLine() Returns the line number of the line of code which threw the exception

Example

Output information about an exception that was thrown:

<?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"
;
}
?>