• Post Reply Bookmark Topic Watch Topic
  • New Topic

Enhancing the use of custom exception and using error codes  RSS feed

 
Saurabh Pillai
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We use custom exceptions in our application. I am in a situation where I am not able to decide whether to create a new custom exception or associate a new property "errorCode" to an existing exception. Here is the situation.

When you search for a user in database and if you find a user, there are 2 possibilities. Either user is active or inactive. This is one of the validations while validating user inputs. I need to take different actions based on user's status. We have an exception "UserFoundException". What I am confused about is, whether I should create two separate exceptions like "ActiveUserFoundException" and "InactiveUserFoundException" OR associate a property called "errorCode" to an exception.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you search a database, you are liable to get an SQLException. Since SQL queries are often accompanied by an error message and an error code if anything goes wrong, SQLException already has those fields. Look through its documentation.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are two different styles, and it's more or less a matter of taste which style you prefer.

Think about the uses. When you would have two different subclasses of UserFoundException, for example ActiveUserFoundException and InactiveUserFoundException, then it would look something like this when you call a method that could throw a UserFoundException.

Since you can have multiple catch blocks for one try block, there's a natural mechanism to keep handling the different conditions separate.

The other possibility would look something like this. I used an enum here for the error code.

I would be in favour for the first solution, and I don't like the way that SQLException works. One of the problems with SQLException is that the error code in the exception is a database vendor specific code, which defeats one of the basic ideas of the JDBC API - it's supposed to be a common API that shields the user from the differences between different database brands. The Spring framework has a mechanism to translate SQLExceptions with error codes to a hierarchy of exception classes (so it has a way to translate from case 2 to case 1).

Another thing: UserFoundException sounds strange. You would say that if you are searching a user in the database, you might get an exception if the user is not found, so you'd expect that there would be a UserNotFoundException.

You might be using exceptions for normal control, which is not the right way to use exceptions. If you are doing that, you should not be using exceptions at all, but something like this.
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hay Jesper nicely put the explanation ..
The Spring framework has a mechanism to translate SQLExceptions with error codes to a hierarchy of exception classes
..Could you please give a little detail on this .

Thanks ,
Satya
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The Spring Framework has many different modules, including a number of modules to work with different database access technologies, including JDBC and JPA (Java Persistence API).

It has a hierarchy of exception classes, at the root of which is DataAccessException.

Suppose that you try to delete some row from a database using plain JDBC. If there's a foreign key that prevents you from deleting a specific row, you would get an SQLException. Just by looking at the type of the exception you can't to see what's wrong, because SQLException is thrown for many different reasons - for example when you can't login to the database, if the SQL statement has a syntax error, if the table you're trying to access does not exist, etc.

To find out what is wrong you would have to call getErrorCode() on the SQLException, which will give you a vendor-specific error code. If you would be using an Oracle database, it would probably be code 2291 or 2292. If you would be using PostgreSQL it would probably be 23503. And if you would be using some other brand of database it would be a totally different code.

It's strange that the designers of the JDBC API chose to do it this way, because one of the main goals of JDBC is to make it so that your code doesn't have to be written for a specific brand of database.

When you use Spring's API for data access instead of using JDBC directly, then Spring will throw one of the exceptions in its DataAccessException hierarchy. If you want to handle a specific case, like an integrity constraint violation, then all you have to do is write a catch block for the appropriate type of exception. In this example it would be a DataIntegrityViolationException. Your code would be shorter and more clear and you wouldn't have to deal with vendor-specific error codes.
 
Saurabh Pillai
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Another thing: UserFoundException sounds strange. You would say that if you are searching a user in the database, you might get an exception if the user is not found, so you'd expect that there would be a UserNotFoundException.


I need to throw UserFoundException at the time of registration. So while registering, if you find email address that already exists, I throw this exception. Now there are 2 possibilities. Either user is active or inactive. I need to execute 2 different set of tasks for each case. I hope I have explained it properly.

Reason I mentioned error code because, I need to use same method as web service. I am thinking about, giving error code to caller instead of whole error message. Does this make sense?

And Thank you for your detailed explanation.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!