aspose file tools*
The moose likes Java in General and the fly likes What is the best way to handle Exception in Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is the best way to handle Exception in Java?" Watch "What is the best way to handle Exception in Java?" New topic
Author

What is the best way to handle Exception in Java?

Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
We are designing an application that will be developed using JSP + EJB .

To deal with the exception part, we have thought about following solutions

(1)All methods in EJB will be declare with throws clause, with the respect to the potential exceptions that can be thrown by method.
We will catch only those exceptions, for which we can take alternative steps or further action when they occurred. All other exceptions will be thrown to the caller.

(2)All JSP pages will have try/catch that catches exceptions thrown by EJB or by JSP code.

(3)A common JSP file will be designated to handle Exception by setting isErrorPage property true and all JSP files are pointing to that particular file for exception handling.

(4)Whenever any exception occurred in application, it will bubbled up and end user will get a common JSP page designed to handle exception.

I am looking for the suggestions to improve this mechanism.

Regards,
Chetan


My blood is tested +ve for Java.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Can I clarify a few things that you are probably planning to do (assuming that the client is remote).

If the EJB catches or creates an application exception, the EJB will throw it as an application exception and the client will handle it (do some recovery operation).

If the EJB is in a transaction context and catches an application exception which makes it a nonsense to continue the transaction, the EJB will call setRollback() and then throw the application exception.

If the EJB catches an exception which the client does not expect or cannot recover from, eg SQLException, it will wrap it and rethrow it as an EJBException.

If a RuntimeException is created in the EJB, the EJB will allow it to propagate to the container (which will then throw a RemoteException to the client).


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Yeah, on the similar path.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the best way to handle Exception in Java?