I am wondering what to do with unexpected exceptions that occur in my business layer. What do you guys think is the best approach?
The reason I am unsure is because if these particular problems occur it implies that something serious has gone wrong with the system.
My particular problem is within the book-method of my Business Service class. When I am about to book a room I first lock the record in the database. Then I read the record back from the database to ensure that it is still unbooked before continuing.
Now, the read method on my database throws a
RecordNotFoundException. In theory this should never get thrown when I am reading back the record as I already own the lock, so no one should have been able to delete the record. My code is like this:
So, if a
RecordNotFoundException is thrown when I read the record after locking it, then something seriously wrong has happened in the system. But what should I do with the exception when I catch it here in my Business Layer?
My initial thoughts were to simply thrown an
IllegalStateException. But this is a runtime exception and my client code will not be set up to handle this - I will just end up with an exception being thrown in the console probably.
Another thought I had was to just throw a general
BusinessException to indicate that something has gone wrong. I already have this exception class in my code. When the client code receives this exception it just tells the user that
"Something unexpected has gone wrong when attempting to book the room, please try again". But would that really be the correct message to send to the user in this particular scenario?