Profile photo
Submitted by Mason Miller
on June 20th, 2011 at 21:52:19 EST

Difference between PHP Error Vs Exception

I am kind of confused between PHP Error and Exception. It sounds similar but can not figure out the difference. What is the main difference between the two?
Thanks in advance.

Profile photo
Replied by SBajra
on June 21st, 2011 at 07:43:04 EST
Even though Error and Exception sounds similar, its two different entities in PHP. Exception can be handled using Try...Catch...block but not Error. You can not handle PHP Error with Try...Catch... block. Any programming or execution error thrown by PHP is Error. Exception on the other hand is mostly used by user class/object, mostly for object oriented programming in PHP. You can use "throw" keyword to throw an Exception. However, you need to use trigger_error() method to generate Error.
In short, when anything is wrong in PHP you will get Error, however, for those who are more confortable with OOP Exception handling mechanism with Try...Catch...Throw etc, there is Exception class as well.
For more details:
Accepted Answer

Profile photo
Replied by Mason Miller
on June 21st, 2011 at 18:34:49 EST
Thanks for nice explanation