?
Solved

EJB unexpected exception

Posted on 2003-03-16
12
Medium Priority
?
356 Views
Last Modified: 2013-11-24
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?
0
Comment
Question by:dannysh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
  • +1
12 Comments
 
LVL 2

Expert Comment

by:functionpointer
ID: 8146930
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
 
LVL 1

Expert Comment

by:Jitu
ID: 8149411
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
 
LVL 2

Expert Comment

by:functionpointer
ID: 8149468
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:dannysh
ID: 8150106
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
 
LVL 35

Expert Comment

by:girionis
ID: 8150958
 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
 
LVL 35

Expert Comment

by:girionis
ID: 8150977
 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
 
LVL 35

Expert Comment

by:girionis
ID: 8150983
 I know that double post sucks but triple is even worse. Sorry for the multiple posts.
0
 

Author Comment

by:dannysh
ID: 8150994
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
 
LVL 1

Expert Comment

by:Jitu
ID: 8150996
girionis >
Do u mean that all business exceptions that are thrown, they should be subclasses of RemoteException class ?
0
 
LVL 35

Accepted Solution

by:
girionis earned 120 total points
ID: 8151121
> 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
 
LVL 35

Expert Comment

by:girionis
ID: 8151160
>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
 
LVL 35

Expert Comment

by:girionis
ID: 8151677
 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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question