PHP Tutorial PHP Advanced PHP References

PHP Exception - getLine() Method



The PHP Exception::getLine() method is used to get the line number where the exception was created.

Syntax

final public Exception::getLine()

Parameters

No parameter is required.

Return Value

Returns the line number where the exception was created.

Example: Exception::getLine() example

The example below shows the usage of Exception::getLine() 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) {
  $line = $e->getLine();
  echo "The exception was created on line: $line";
}
?>

The output of the above code will be:

The exception was created on line: 4

❮ PHP - Exceptions