Using a code for an exception. Useful?

exception, php

Solution

The error code was a feature used when there was no object oriented language. The only thing that could aid you to understand what went wrong was the error code. In an object oriented language, the object IS your error code. Unless, in specific cases, more than one thing can throw the exact same error AND they are treated in different ways, drop it. Also, you would provide much better explanation to whomever is debugging your code if you left a message instead of a meaningless error code, so if you feel like the exception needs more information, fill the Error Message field instead.

Problem

I am not sure if Exceptions work the same way in each language, but I am using PHP and I was wondering when I'm doing something like this: ``` if (!$this->connection[0]->query($this->query)) throw new QueryFailedException($this->connection[0]->error); ``` Is there a need to supply a code in the second parameter? For example: ``` if (!$this->connection[0]->query($this->query)) throw new QueryFailedException($this->connection[0]->error,123); ``` Now the code is 123... I can't think of a need for this. Is there one? In this case the message contains the query, exception name is QueryFailedException which explains the exception type, the exception itself contains file, line and stack trace, so, I can't think of anything where you could use the code for something useful.

Original source

Related problems