Reputation: 21698
What is the exact semantic difference between \Exception
and \RuntimeException
in PHP? When we should use the former and when the latter?
Upvotes: 36
Views: 24194
Reputation: 81
If we’re talking about the semantic difference, I would say that \Exception
doesn’t have any semantics. The \Exception
is so general that we can only say that this is an exception and nothing else. We cannot even say what this exception is related to if we don't have any additional context. On the contrary, the \RuntimeException
is more specific and, thereby, it has some semantics.
According to the RuntimeException class documentation Exception thrown if an error which can only be found on runtime occurs.
. So, the semantics is that this exception is strongly related to the runtime. To me, it means that the problem happens during the execution stage and mostly has dynamic nature.
Upvotes: 1
Reputation: 10643
RuntimeException is thrown if an error which can only be found on runtime occurs.
The key word here is found. Some summaries say that a RuntimeException happens when the program is running. That's redundant: all errors & exceptions happen when the program is running. The key difference is that a RuntimeException is caused by bad data, not by bad coding logic, so it cannot be found by analysing the code before it is run.
That said, there is no technical difference between them: RuntimeException inherits from Exception. But keeping them separate may help you to keep the logic clear in your own brain (i.e., the difference is semantic), and to catch different types of exceptions in different places, and deal with them separately.
You can always create your own exception classes, for similar semantic meanings to further subdivide your exceptions, or to actually add extra functionality. Your own exception classes may inherit from RuntimeException or from LogicException, or more simply directly from Exception. Inheriting directly from Exception seems to be the norm. Think about it and do what makes sense to you. Once again, there is no difference in how PHP itself treats these exception classes.
Upvotes: 3
Reputation: 1499
The only difference between those two is semantic. The \RuntimeException
inherits from \Exception
. Basically there are no other differences.
You can create your own exceptions inheriting from both of above, the most common usage is to inheritit from \Exception
.
Upvotes: 4
Reputation: 1112
Exception is a base class of all exceptions in PHP (including RuntimeException). As the documentation says:
RuntimeException is thrown if an error which can only be found on runtime occurs.
It means that whenever You are expecting something that normally should work, to go wrong eg: division by zero or array index out of range etc. You can throw RuntimeException.
As for Exception, it is a very generic exception and I would call it a "last resort". You can add it as a last one in "try" just to be sure You are handling all exceptions.
Example:
try {
//code...
} catch(RuntimeException $e) {
echo ("RuntimeException...");
} catch(Exception $e) {
echo ("Error something went wrong!");
var_dump($e);
}
Hope it is a clear now.
Upvotes: 41