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.