PHP Tutorial PHP Advanced PHP References

PHP Exception - getFile() Method



The PHP Exception::getFile() method is used to get the name of the file in which the exception was created.

Syntax

final public Exception::getFile()

Parameters

No parameter is required.

Return Value

Returns the filename in which the exception was created.

Example: Exception::getFile() example

The example below shows the usage of Exception::getFile() method.

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

try {
  echo divide(25, 0);
} catch(Exception $e) {
  $file = $e->getFile();
  echo $file;
}
?>

The output of the above code will be similar to:

Main.php

❮ PHP - Exceptions