File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Application Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Application Exceptions" Watch "Application Exceptions" New topic
Author

Application Exceptions

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

Hello,
In EJB, the Application exceptions and system exceptions are handled differently. System exceptions are wrapped in RunTimeException, while application exceptions are thrown back as it is. This is because, we always have our Application exceptions as checked exceptions.

Now,lets say I have a method which takes an integer argument, and if it is less than 1000, I want to throw back an exception. Instead of writing a new exception, I decide to use IllegalArgumentException. Also, in the method signature, in the throws clause I specify this(though not required as it is an RunTimeException). But now, as the container sees this as an RunTimeException, it throws me back an RunTimeException and not IllegalArgumentException as I wanted.

So, why is it designed this way?
I end up writing lot of custom exceptions because of this. What is the way out?

Thank You,
Ranga.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Andrea Gazzarini
Ranch Hand

Joined: Sep 09, 2002
Posts: 125
Hello,
you say : "System exceptions are wrapped in RunTimeException"
No, the system exceptions ARE RuntimeException, so when the Bean throws a RuntimeException, the container wraps it into an EJBException.
Then, if the client is local, it receives the EJBException (or a TransactionRolledBackLocalException), while if it is remote, it receive a RemoteException (or a TransactionRolledBackException)

A System exception is a non-recoverable situation where you haven't a fine-grained control, so you simply can throw an EJBException...but if the IllegalArgumentException is the cause of a missing business requirements, you must signal it with an application exception...

Ciao


Regards,<br />Andrea<br /> <br /><a href="http://www.linkedin.com/in/andreagazzarini" target="_blank" rel="nofollow">Andrea Gazzarini</a><br />Skype ID : gazzax72<br />Google Talk ID : a.gazzarini@gmail.com<br /><a href="http://www.andreagazzarini.blogspot.com" target="_blank" rel="nofollow">My Blog</a>
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Application Exceptions