This discussion is archived
6 Replies Latest reply: Jul 4, 2012 8:44 AM by r035198x RSS

Exception throwing in an EJB method

srhcan Newbie
Currently Being Moderated
Hi guys,

I have a Stateless Session Bean named TimeRules [EJB 2.1] where I am putting my business methods. I am concerned about what exception should each of these methods throw? I think I have 2 choices:

1. Each method should throw javax.ejb.EJBException.

2. Create my own Exception TimeRulesException inherited from java.lang.Exception; then each method should throw TimeRulesException

Which one is preferable and why? Are there any more choices which makes more sense than the above 2?

Thanks

Edited by: srhcan on Jul 4, 2012 10:44 AM
  • 1. Re: Exception throwing in an EJB method
    r035198x Pro
    Currently Being Moderated
    The container will wrap your exception in an EJBException so there is no reason why you should throw it yourself from your code.
    Separate your business from your infrastructure so your business services should throw business exceptions. The infrastructure will be responsible for throwing infrastructure exceptions.
  • 2. Re: Exception throwing in an EJB method
    srhcan Newbie
    Currently Being Moderated
    r035198x wrote:
    The container will wrap your exception in an EJBException so there is no reason why you should throw it yourself from your code.
    Separate your business from your infrastructure so your business services should throw business exceptions. The infrastructure will be responsible for throwing infrastructure exceptions.
    I am confused regarding difference between infrastructure exceptions and business exceptions.
    If one EJB business method is inserting a row in a table and it failed for some reason (PK or FK violation for example) then is it a infrastructure exception (EJBException) or a business exception (TimeRulesException)?
  • 3. Re: Exception throwing in an EJB method
    r035198x Pro
    Currently Being Moderated
    EJBException talks about the technology being used not the business use case being solved. Your Exceptions in your business code should indicate the business scenarios not the technologies being used to realize those use caes.
    Failing because of FK violations can happen whether the call was done from an EJB or someother technology so the exception should not be tied to EJB technology only. When you use EJB technology the container will wrap your custom exception in an EJBException anyway.
  • 4. Re: Exception throwing in an EJB method
    srhcan Newbie
    Currently Being Moderated
    r035198x wrote:
    EJBException talks about the technology being used not the business use case being solved. Your Exceptions in your business code should indicate the business scenarios not the technologies being used to realize those use caes.
    Failing because of FK violations can happen whether the call was done from an EJB or someother technology so the exception should not be tied to EJB technology only. When you use EJB technology the container will wrap your custom exception in an EJBException anyway.
    yes it makes sense that an insert table failure (SQLException) is a business exception. But I have seen code on internet where they are wrapping SQLException in a EJBException and thats is getting me confused. For example look at this code on J2EE Tutorial
    public void transferToSaving(double amount) throws
       InsufficientBalanceException  {
    
       checkingBalance -= amount;
       savingBalance += amount;
    
       try {
          updateChecking(checkingBalance);
          if (checkingBalance < 0.00) {
             context.setRollbackOnly();
             throw new InsufficientBalanceException();
          }
          updateSaving(savingBalance);
       } catch (SQLException ex) {
           throw new EJBException 
              ("Transaction failed due to SQLException: " 
              + ex.getMessage());
       }
    }
    http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Transaction3.html
  • 5. Re: Exception throwing in an EJB method
    gimbal2 Guru
    Currently Being Moderated
    EJBException would be nothing special, but it just so happens to be that it is a RuntimeException in stead of a regular Exception. At this point a lightbulb should be popping on over your head. If you don't know why that is important you have plenty more reading to do before you should be asking questions in a forum because you lack some very important fundamentals of EJB technology, including transaction management.

    Assuming it is dark above your head: A RuntimeException (which includes EJBException) when thrown from an EJB method will cause a transaction rollback. Any exception that is not a runtime exception will in fact not trigger an automatic rollback. That's a mighty tool at your disposal that you should use to good effect and with great care.

    You can of course define your own exceptions, including those which derive from RuntimeException (and this will trigger a rollback). A good reason to be doing that is for code documentation purposes. That's a highly personal opinion though, there is no big book of rules that will dictate how you should be doing your things.
  • 6. Re: Exception throwing in an EJB method
    r035198x Pro
    Currently Being Moderated
    For ejb 2.1 you should use the section http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/BMP6.html#63681
    to understand how to use exceptions in that spec version.

    Note the difference between that SQLException and the business exception thrown earlier in the method. The SQL exception is being treated as a system exception rather than a business exception.
    In your FK example, you may want to throw that as an application exception instead if the FK semantics are within your business rules.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points