r/PHPhelp • u/Spiritual_Cycle_3263 • Apr 27 '26
Runtime vs InvalidArgument exceptions - when to use which?
When should you throw RuntimeException vs InvalidArgumentException in PHP?
I understand that InvalidArgumentException extends LogicException, which is meant for errors detectable before runtime, and RuntimeException is for conditions only discoverable during execution.
But I keep second-guessing myself on cases like this:
function getFile(string $path): string
{
if (!file_exists($path)) {
throw new ???Exception('File not found.');
}
return file_get_contents($path);
}
The argument $path is technically valid -- a non-empty string, correct type. But the file it refers to does not exist. Is that a RuntimeException because the absence is only discoverable at runtime? Or is it an InvalidArgumentException because the argument I passed did not correspond to anything real?
Where do you draw the line, and how do you think about this distinction?
3
u/HolyGonzo Apr 28 '26
I would use RuntimeException for your specific situation because it's something that is ONLY detectable at runtime. If someone creates the path and you re-run the exact same code with the same path, it will work.
RuntimeException is specifically for that kind of situation. However, most frameworks create their own "FileNotFoundException" class just to make it extremely clear.
4
u/sporadicPenguin Apr 28 '26
It’s neither IMO. Either throw a generic exception or create your own.
Also you want to catch errors with file_get_contents(), as there are ways that will fail even if file_exists() returns true.
1
u/ElectronicOutcome291 Apr 28 '26
Nice ask and a good question - i would tend to Go for the RuntimeException - since path is valid as you Said. If i would pass SplFileinfo instead of a String, i probably would Go for invalidArgument.
In most cases i would omit throwing a exception alltogether in this function tbh. Validating and reading are two different Things. (Omit it by reducing it to a one liner, EG: (Return File_getc() ?? '')
In most cases i will use structures that rely on the DirectoryIterator ( for retrieving $path ) that results in dealing with SplFileinfo that would call the equivalent as in the given function. A neat Trick is "(string)$splFileinfo" to avoid extra Overhead that is present while using "->getRealpath()"
1
u/martinbean Apr 28 '26
I tend to favour
RuntimeExceptionas most exception classes I create, are to be thrown when I’m expecting something not to work during runtime (e.g. a file not existing, an external API call failing, etc) and want it logging in Sentry so I know to either fix the problem if it’s fixable, or what to recover if it’s temporary.1
u/colshrapnel Apr 28 '26 edited Apr 28 '26
Iiiiiii think I understand where your logic goes wrong. In a way, indeed it can be called invalid argument exception, because the argument used didn't work. But it would be too generic. This way, any malfunction happened in a function can be blamed for the invalid argument, because most functions rely on their arguments.
But when talking of invalid arguments, it's about the argument itself, not its subsequent use.
Like, the path itself could be correct, but the file was deleted by admin. So it's not the argument to blame, see?
Therefore, in order to throw the InvalidArgumentException, you need to scrutinize the argument itself. Like, assuming it cannot be empty, you can rightfully make it
though I wouldn't really bother, since such a validation won't add any real value, but would just clutter your code.
-3
u/arm14789 Apr 28 '26
I wouldn’t throw an exception here 😄
If the file is found, just return the content; otherwise return false then check if it is false you know what to do in your code. Using too many try/catch blocks can make the code harder to read.
Regarding when to use what kind of exception i agree with others