Okay, its a very lame question for many but I hope I will have overwhelming response 🙂
When I throw an Exception in PHP I can add a code to the message.
I catch an exception and handle it according to its type (Like
OutOfBoundException). I log the message or display it or do whatever is suitable.
I can add also append a previous exception to trace a path to the origin of the error.
BUT one thing I have never used or never thought of: how useful is code?
throw new Exception("db Error", $code, $previousException);
What do I do with
Here is Solutions:
We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.
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.
$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
$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.
I’ve seen implementations (CakePHP) where the
$code is used as HTTP status code.
I’ve implemented that concept with a subset of exceptions. So all exceptions extending from
HttpException which are thrown respond with HTTP errors
In object oriented languages, the type of the exception conveys what type of error it is. However, if for example you have two things that can generate the same exception type, the error code could be used to give more detail.
The error code is a widely used feature in non-object oriented language to convey what type of error it is.
I personally use the code to get a compressed error message, that user can send to the support. For example, let’s say the user tries to authenticate, and he fails, my code throws an
AuthenticateException with the message:
Failed to authenticate, and a specific code referring to the real issue behind the failure. The user will only see the authentication message, and the code, thus not knowing what the real reason of the failed authentication was. He is then advised, that if needed, contact the support team with the code.
Based on the exception code, our support colleagues can easily pin-point what was the real reason for the failed authentication (invalid password, inexistent user-name, account was suspended, etc.) and may help the user accordingly.
Lots of good answers so far, but I’m surprised none have hit upon the core of what an error code really is.
You need an error code for the same reason you need an id column in a database table. It’s a unique identifier for the exact error that happened.
The name of the Exception type is not the same. For example:
- There could be multiple variations or causes of an exception with the same type. The error code might give you more information if you look it up than just the type of error.
- When you manually throw an error in code, there’s a lot of positives to being able to pass a custom error code with it. You might want to identify the exact error or place in the code it happened without creating a new type for every exception in your code example.
- When you need to check an error object for what error happened, doing a simple
if ($error->code === 201)is a lot easier than checking the type or comparing error message strings. If you don’t want to use 201 in your code because it doesn’t convey the error, it’s also really easy to assign it to a constant.
- If you want to log information about an error in a database, having a unique identifier is a lot easier to work with in a database. And then when you pull it out of the database to work with it in code (no longer in an Exception object), it’s again easier to identify the error by an error code and work with it that way. If you store something like a class name in the database, what if someone puts it in the database with camelCase as opposed to PascalCase, or they remember the exact name wrong, or they have a typo, etc. Even if you use an enum, what if you need to interface with a different database that might have used a different format?
- If you give an error code to a user in your application, and he goes to a page with a list of errors, it’s a lot easier for him to find the exact error with an error code than to look through text descriptions of the error. It’s also easier for them to find it in a search engine.
Basically having a global error code makes it easy to store, reference, pass, compare, change formats without losing information, share code and data with others, create custom errors, etc. Most things that apply to storing data in a database with a unique id also applies to storing an error with an error code.
Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂