Understanding the Exception::getline() Method in PHP
At times, errors and exceptions can occur in PHP code, and as a developer, it's crucial to handle these errors appropriately. PHP provides a range of exception
When something goes wrong in PHP, the first thing you usually need to know is where it went wrong. The Exception::getLine() method answers exactly that: it tells you the line number on which an exception object was created. Paired with getMessage() and getFile(), it gives you a precise "what, where" for any error you catch — which is the foundation of useful logging and debugging.
This page covers the method's signature, what it actually measures, a complete runnable example, and the common pitfalls that trip people up.
Syntax
final public Exception::getLine(): intThe method takes no arguments and returns an int. It is defined on the base Exception class, so every built-in exception (RuntimeException, InvalidArgumentException, TypeError, and so on) and every custom exception you write inherits it. Because it is declared final, you cannot override it in a subclass.
What "line" actually means
This is the single most important thing to understand: getLine() returns the line where the exception object was constructed (the new Exception(...) / throw new ... site), not the line of the underlying problem and not the line where it was caught.
So if your code calls a function that internally throws, getLine() points at the throw statement inside that function — not at your call. To trace the full path from your call down to the throw, use getTrace() or getTraceAsString() instead.
A complete, runnable example
The example below defines a function that throws when a file is missing, then catches the exception and reports the location:
<?php
function readFileContents(string $path): string
{
if (!file_exists($path)) {
throw new Exception("File not found: $path"); // this is line 6
}
return file_get_contents($path);
}
try {
echo readFileContents('/no/such/file.txt');
} catch (Exception $e) {
echo "Error on line " . $e->getLine() . ": " . $e->getMessage();
}Output:
Error on line 6: File not found: /no/such/file.txtNotice that the reported line is 6 — the throw statement inside readFileContents() — even though the call that triggered it is on line 13. That is the "construction site, not the call site" rule in action.
Combining getLine() with getFile()
A line number alone is ambiguous in a multi-file project, so in real code you almost always print it together with the file name from getFile():
<?php
try {
$age = -5;
if ($age < 0) {
throw new InvalidArgumentException("Age cannot be negative");
}
} catch (Exception $e) {
printf(
"[%s] %s (line %d in %s)\n",
get_class($e),
$e->getMessage(),
$e->getLine(),
basename($e->getFile())
);
}Output (the file name depends on what you name the script):
[InvalidArgumentException] Age cannot be negative (line 5 in script.php)Here getLine() returns 5, the line of the throw inside the try block. Catching Exception works for InvalidArgumentException because every exception type ultimately extends Exception.
When to use it
- Logging. Write
getLine()andgetFile()into your log entries so a stack-trace-free message is still traceable. - Custom error pages. In a development environment you might surface the line to the developer; in production, log it but show the user a generic message.
- Re-wrapping exceptions. When you catch a low-level exception and re-throw a domain-specific one, record the original
getLine()/getFile()(or chain withgetPrevious()) so the root cause is not lost.
Common pitfalls
- Expecting the call site. As shown above,
getLine()is the throw location, not where you called the throwing code. Use a trace for the full path. - Reading it without catching.
getLine()is an instance method — you can only call it on an exception object you have a reference to, typically the$efrom acatchblock. - Confusing it with
getCode().getCode()returns the exception's numeric code, an unrelated value you set when constructing the exception; it has nothing to do with line numbers.
Conclusion
Exception::getLine() returns the integer line number where an exception was created — a small but essential piece of debugging information. Used together with getMessage(), getFile(), and a full stack trace, it lets you pinpoint and log errors precisely. For the bigger picture of throwing and catching, see the guide to PHP exceptions.