getFile()
Learn how PHP's Exception::getFile() returns the absolute path of the file where an exception was thrown, with examples and usage tips for debugging.
Introduction
getFile() is a built-in method of PHP's Exception class (and every class that extends it, including Error). When an exception is thrown, PHP records the path of the source file in which the throw statement ran. getFile() returns that path so you can pinpoint where something went wrong — which is invaluable when an error bubbles up through many layers of code.
This page covers what getFile() returns, how it differs from related methods, and how to combine it with getLine() and getMessage() to build readable error logs.
Syntax
public string Exception::getFile(): stringgetFile() is declared final on the base Exception class, so you cannot override it in your own exception subclasses — the value is always set by PHP itself at the moment the object is constructed.
Parameters
getFile() takes no parameters.
Return value
A string containing the absolute path of the file in which the exception was thrown — for example /var/www/app/src/Order.php, not just Order.php. If you only want the file name, wrap the result in basename().
The path reflects where the exception object was created (the throw/new site), not where it was caught. This distinction matters when you re-throw exceptions across files.
Basic example
The example below throws an exception and then reports exactly where it came from:
<?php
try {
throw new Exception('Database connection failed');
} catch (Exception $e) {
echo 'Message: ', $e->getMessage(), "\n";
echo 'File: ', $e->getFile(), "\n";
echo 'Line: ', $e->getLine(), "\n";
}Output (the path depends on where the script lives):
Message: Database connection failed
File: /var/www/app/index.php
Line: 4Notice that getFile() reports line 4 via getLine() — the line of the throw, not the catch.
Building a readable log line
In real applications the file path is usually long, so it is common to trim it with basename() and assemble a single, compact log message:
<?php
function processOrder(int $id): void
{
throw new RuntimeException("Order #$id is invalid");
}
try {
processOrder(42);
} catch (RuntimeException $e) {
echo 'Error in ' . basename($e->getFile())
. ' (line ' . $e->getLine() . '): '
. $e->getMessage() . "\n";
}Output:
Error in index.php (line 5): Order #42 is invalidEven though the exception was caught inside the try block, getFile() and getLine() point at line 5 — the throw statement inside processOrder().
When to use it
- Logging and monitoring. Combine
getFile()withgetLine()andgetMessage()to write log entries that tell you exactly where to look. - Debugging deeply nested calls. When an exception travels up through several functions or files,
getFile()tells you the true origin, not the catch site. - Custom error pages (in development). Display the file and line on a debug screen — but never expose absolute server paths to end users in production.
For the full picture of an error you will often pair it with getTrace() or getTraceAsString(), which show the entire call stack rather than a single location.
Related methods
| Method | Returns |
|---|---|
getMessage() | The human-readable error message |
getLine() | The line number where the exception was thrown |
getCode() | The exception's numeric code |
getFile() | The file path where the exception was thrown |
See getMessage(), getLine(), and getCode() for each of these. To learn how the whole mechanism fits together, read the PHP exceptions chapter and the try/catch reference.
Conclusion
getFile() is a small but essential part of PHP's exception system: it answers the question "which file threw this?" Together with getLine() and getMessage() it turns a vague failure into a precise, actionable report — the foundation of good logging and fast debugging.