What do we need the PHP-exception code for? Any use case scenario?
How $code
is interpreted is dependent on the exception type. For example, if you have an Exception
subclass that represents a MySQL database error, then the $code
could be the native MySQL error code. In the case of a low-level IO error, this could be a value from <errno.h>
.
Basically, $code
should contain whatever you need to programmatically handle an exception. Most exceptions are meant to be handled somewhere. If all of your exceptions are simply displayed as errors, then $code
is only useful if you need to include an error code from a library like the MySQL client library.
The message is for display to the user, while the code is for use by your program. So for example, in your "database error" example, you might make up a set of codes like
- Can't connect
- Error during query
- Empty result
- Error closing connection
and then use the appropriate code. Then when other parts of your code saw they exception, they would know what happened and could possibly deal with it intelligently.