jQuery in Action, 3rd edition
The moose likes EJB and other Java EE Technologies and the fly likes Unable to catch SQLException in EJB Client... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Unable to catch SQLException in EJB Client..." Watch "Unable to catch SQLException in EJB Client..." New topic

Unable to catch SQLException in EJB Client...

Pras Tiwari
Ranch Hand

Joined: Nov 07, 2005
Posts: 186

I have developed successfully EJB's for my application. Now inside my client I am accessing one of my bean & while creating bean inside client it throws SQLException. But inside my client my Catch block with "CreateException" catches that exception though I have written a catch block with "SQLException". My create method inside EJB as follows:-
public CartMasterPK ejbCreate(Timestamp Cartdate, String Customerid,Double Totalvalue, String Status) throws CreateException, SQLException {

& code inside my client which is accesiong create method is as follows:-
try {
cmaster=cmhome.create(new Timestamp(System.currentTimeMillis()),request.getParameter("customerid"),new Double(request.getParameter("totalvalue")),request.getParameter("status"));
catch (RemoteException e) {
catch (SQLException e) {
catch (CreateException e) {

Now according to above when create method throes SQLException it should be catched by Catch(SQLException e) block. But here in my client it get cought by catch (CreateException e) block.

Where is the problem???

Also is it necessry that both ejbCreate method & ejbPostCreate method should throw same number of esceptions???

Please help me.

Thanx in advance.


********Deserve Before You Desire********
Chris Brat
Ranch Hand

Joined: May 22, 2003
Posts: 108

You have to read the spec - different exceptions will be returned to the client depending on whether :

- the client is local or remote
- the exception is an application exception (expected) or a system (unexcepted exception such as a NullPointer).
- the operation that is being performed (create, remote, find etc.)

Take a look at this tutorial


MI Mohammed
Ranch Hand

Joined: Feb 16, 2005
Posts: 146
Hi Tiwari,
What you have to do is wrap the SQLException and throw it as EJBException.
It the client is remote, the container will pass a RemoteException and for local client, they will get EJBExecepion.

catch( SQLException sqlex) {
// do whatever thing you want to
throw new EJBException();
Hope it helps.

SCJA(Beta) SCJP 1.4 SCWCD 1.4 SCBCD 1.3 SCBCD 5.0 beta <br />The more practice we get, the better we are at the exams and in life in general. Pls join me at My DEN.
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
Note that if this entity bean is a CMP bean, you aren't ever going to get a SQLException. One might be chained to the relevant javax.ejb.* exception you get, but getting SQLException itself isn't possible unless you are using a BMP bean that causes SQLExceptions to be thrown.

Also, as a rule of thumb, propagating SQLExceptions outside of an entity bean is a questionable practice. Clients of a bean shouldn't know that much about its internals, and what exactly would a client *do* about a SQLException anyways? Better to wrap it in something more consistent with the API, like a standard EJB exception like CreateException or a generic application-specific exception.
[ March 09, 2006: Message edited by: Reid M. Pinchback ]

Reid - SCJP2 (April 2002)
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
For an entity bean, the only exception that must be declared in ejbCreate is CreateException plus any other exceptions declared in the matching create method of the home interface - but not RemoteException. As you have declared SQLException in ejbCreate, then it must be also be declared in create - an undesirable practice as Reid says.

If only CreateException is declared, then only CreateException can be thrown by the bean's implemented class. This will include situations when SQLException is encountered.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
I agree. Here's the link: http://aspose.com/file-tools
subject: Unable to catch SQLException in EJB Client...
It's not a secret anymore!