Rahul Prasad
Rahul Prasad

Reputation: 8222

What do we need the PHP-exception code for? Any use case scenario?

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 InvalidArgumentException or 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?

For example:

throw new Exception("db Error", $code, $previousException);

What do I do with $code?

Upvotes: 56

Views: 25595

Answers (6)

dallin
dallin

Reputation: 9426

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.

Upvotes: 3

Adam Baranyai
Adam Baranyai

Reputation: 3867

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.

Upvotes: 2

Razan Paul
Razan Paul

Reputation: 13838

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.

Upvotes: 3

Koen.
Koen.

Reputation: 26949

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

Upvotes: 3

D.Shawley
D.Shawley

Reputation: 59563

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.

Upvotes: 14

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

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

  1. Can't connect
  2. Error during query
  3. Empty result
  4. 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.

Upvotes: 49

Related Questions