EJB unexpected exception

Hi All
How should I handled unexpected exceptions inside an EJB code?
suppose method inside EJB:
 
  public void method(...)
  {
     try {
     }
     catch(BlaException ...) {
     }
     catch (Exception exp) {
     }
  }

should the code looks like I wrote it? and should I catch Exception and throw from it RemoteException?
dannyshAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
girionisConnect With a Mentor Commented:
> What do u mean application-level exceptions and System-level exceptions ?

  System-level exception are exceptions that are thrown to indicate somethign went wrong with the system (they are usualyl fatal) such as database mulfunction for instance.

  Application-level exceptions are (I guess) what you call business-level exceptions.

  Look at Jitu's first comments, he explains it nicely.

> my bean declared as throw myException with extend java.io.Serializable and not from RemoteException and from what
> you wrote, it seems impossible doing it am I right? Should all exceptions throws from a Bean extends RemoteException?

  and

> Do u mean that all business exceptions that are thrown, they should be subclasses of RemoteException class ?

  No sorry, I should have clarified it. Application level exceptions are always thrown back to the client. However system-level exceptions do not need to be thorwn to the client. If you want the client to know (for instance) that the disk is full (which is a system-level exception) you need to indicate it to the client by throwing a RemoteException or a subclass of it.

>>It depends on the severity of the exception and on the container's architecture
>can it cause the bean to terminate?

  Yes it can.
0
 
functionpointerCommented:
just throw whatever exception you want. There's no reason why BlaException cant be thrown by the ejb, as long as its in the client jar.  I like my EJB's to throw one type of exception ( as well as the RemoteException ) and let the client jar contain this. Any exceptions i use in my ejb extend this.
0
 
JituCommented:
As far as i know,
The only exceptions that must be thrown to the client are the once that will make sense for the client to catch.

So, if you have a SQLException inside EJB, you can just throw RemoteException to the client. Because, the client has no use knowing that it was SQLException, it is enough for it to know that there was a Exception.

But, if you have a business exception, like UserDoesNotExistException, then you may want to throw this exception to the client. Because, the client may want to use this information to do something (eg: alert the user).

public void method(...)
 {
    try {
    }
    catch(SQLException ...) {
      throw new RemoteException();
    }
    catch (UserNotFoundException exp) {
       throw exp;
    }
 }


0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
functionpointerCommented:
IMO, i dont think you should ever PLAN on throwing a RemoteException. If the exception was worth throwing, it was worth including some data. In the following:

public void method(...)
{
   try {
   }
   catch(SQLException ...) {
     throw new RemoteException();
   }
   catch (UserNotFoundException exp) {
      throw exp;
   }
}

assuming method's function was to lookup a user, a SQLException constitutes throwing something like a "LookUpFailureException" or at the very least, some kind of "MyEjbServiceException", root cause being the SQLException. More like:

public void method(...) throws MyServiceException
{
   try {
   }
   catch(SQLException sqle ) {
     throw new MyServiceException("Database error");
   }
// dont bother catching.. UserNotFoundException should extend MyServiceException...
//   catch (UserNotFoundException exp) {
//      throw exp;
//   }
}
0
 
dannyshAuthor Commented:
Hi both

Thanks for your help, What I still don't understand is what kind of exception should the bean throw when the exception accured is unxpected.

  try {
  }
  catch (BusinessException exp) {
     throw exp
  }
  catch (Exception e) {
    //what should I throw here?
  }

should I throw from the Exception block a RemoteException or should I throw other exception? what will happen to the EJB if I will not catch Exception addition to the bussiness exceptions?
   Thnaks
0
 
girionisCommented:
 Only throw application-level exceptions to the client alongside with CreateException and FindException. Keep System-level exceptions to the EJB itself since the client rarely needs to know about them. Even if the client does need to know about system-level exceptions there is nothing the client can do about them.

  More sophisticated containers provide transparent fail-over for system-level exceptions, that is, when a critical exception happens it could be possible to save the client's invocation and redirect it to another bean.

> should I throw from the Exception block a RemoteException or should I throw other exception?

  If you want the client to be aware of the exception then throw a remote exception. If an exception is thrown to the client it is always thrown as a remote excpetion (or a subclass of it).

> what will happen to the EJB if I will not catch Exception addition to the bussiness exceptions?

  It depends on the severity of the exception and on the container's architecture (see my comment about transparent fail-over). It could fail or it could go on normal.
0
 
girionisCommented:
 Only throw application-level exceptions to the client alongside with CreateException and FindException. Keep System-level exceptions to the EJB itself since the client rarely needs to know about them. Even if the client does need to know about system-level exceptions there is nothing the client can do about them.

  More sophisticated containers provide transparent fail-over for system-level exceptions, that is, when a critical exception happens it could be possible to save the client's invocation and redirect it to another bean.

> should I throw from the Exception block a RemoteException or should I throw other exception?

  If you want the client to be aware of the exception then throw a remote exception. If an exception is thrown to the client it is always thrown as a remote excpetion (or a subclass of it).

> what will happen to the EJB if I will not catch Exception addition to the bussiness exceptions?

  It depends on the severity of the exception and on the container's architecture (see my comment about transparent fail-over). It could fail or it could go on normal.
0
 
girionisCommented:
 I know that double post sucks but triple is even worse. Sorry for the multiple posts.
0
 
dannyshAuthor Commented:
Hi girionis

What do u mean application-level exceptions and System-level exceptions ?
>>If an exception is thrown to the client it is always thrown as a remote excpetion (or a subclass of it).

my bean declared as throw myException with extend java.io.Serializable and not from RemoteException and from what you wrote, it seems impossible doing it am I right? Should all exceptions throws from a Bean extends RemoteException?

>>It depends on the severity of the exception and on the container's architecture

can it cause the bean to terminate?

   Thanks
0
 
JituCommented:
girionis >
Do u mean that all business exceptions that are thrown, they should be subclasses of RemoteException class ?
0
 
girionisCommented:
>No sorry, I should have clarified it. Application level exceptions are always thrown
> back to the client. However system-level exceptions do not need to be thorwn to the client. If you want the client to know (for instance) that the disk is full (which is a system-level exception) you need to indicate it to the client by throwing a
> RemoteException or a subclass of it.

  ... that's why I said in my previous message "If you want the client to be aware of the exception" since the client is always aware of application-level exception but does not need to know about system-level exceptions.
0
 
girionisCommented:
 Thank you for the points. I feel like a points thief though since Jitu and functionapointer helped as well. Guys I will post a question for each one of you with the same points :-)
0
All Courses

From novice to tech pro — start learning today.