• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Handling Exceptions in Business Layer

 
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I throw an IllegalStateException and documented this should never occur
 
Sean Keane
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds good. That was my initial approach. Then I started thinking about it a bit more. Bit this approach of an IllegalStateException is probably good enough for this assignment.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I throw an IllegalStateException and documented this should never occur



But in my case, code-which-can-throw-IllegalStateException is inside lock method, which can only throw RNFE (interface restrictions). Should I swallow the IllegalStateException? What can be done in such case?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a SCJP you should know that an interface only restricts the checked exceptions being thrown, but that you can throw any runtime exception you want. And the IllegalStateException is a runtime exception.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also use assertion.

 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Savas Aydin wrote:You can also use assertion.


That's true. But you should not forget to run the application with assertions enabled, otherwise you won't notice you have a bug in your code. So if you use assertions throughout your code that won't be a problem. But if you use only assertions in this case, you might forget it and never notice you have written a bug.
 
reply
    Bookmark Topic Watch Topic
  • New Topic