App-Exceptions not differentiated in client catch blocks

Discussions

EJB programming & troubleshooting: App-Exceptions not differentiated in client catch blocks

  1. I'm using WebLogic 5.1.0.
    My problem is that when I throw a custom exception in my EJB code, the client servlet receives the exception but can only catch it as a generic Exception rather than as the custom exception. My custom exceptions extend a super-exception class which extends the Exception class.

    e.g.

    java.lang.Exception
           |
           +------ MySuperException
                          |
                          +------ MyException1
                          +------ MyException2
                          +------ MyException3

    For debugging purposes, I print out the exception received (exception.getClass().toString()) and it does show my custom exception, with the exception message intact!
    The client code is as follows:

    try {
        myEJBClass.generateMyException1();

    } catch(MyException1 e) {
        // The code never gets here!!
        handleMyException1();

    } catch(MyException2 e) {
        handleMyException2();

    } catch(MyException3 e) {
        handleMyException3();

    } catch (MySuperException e) {
        // The code never gets here either!!
        handleMySuperException();

    } catch (Exception e) {
        // The code does get here!
        String eType = e.getClass().toString();
        System.out.println("Class of exception = " + eType);
        System.out.println("Exception: " + e.getMessage());
        
        // Believe it or not, this works :-0
        // This is a kludge I'd rather not use!
        //
        if (eType.endsWith("MyException1")) {
            handleException1();

        } else if (eType.endsWith("MyException2")) {
            handleException2();

        } else if (eType.endsWith("MyException3")) {
            handleException3();

        } else if (eType.endsWith("MySuperException")) {
            handleSuperException();

        } else {
            reportError();
        }
    }


    Can anyone suggest why my custom exceptions cannot be caught in a more orthodox fashion?

    Tod

  2. All application exceptions occured in EJB will be wrapped with RemoteException and throwed to client. So in your client servlet you can only catch the RemoteException.
  3. This isn't correct...if you throw an application exception (e.g., MyBusinessLogicException) the container should rethrow this exception, unless the container is not following the specs or there's a bug.

    --
    Tinou
    www.tinou.com
  4. I tested your theory by adding the following code just before the Exception catch block:

    } catch (RemoteException re) {
        // The code does NOT get here!
        System.out.println("RemoteException: "
                         + re.toString());
    } catch (Exception e) {
        // The code does get here!
        String eType = e.getClass().toString();
        System.out.println("Class of exception = " + eType);
        System.out.println("Exception: " + e.getMessage());

    The program still went to the Exception catch block, which tells me that if the underlying exception was wrapped by a remote exception, it was unwrapped once marshalled back to the client.

    Any other thoughts?

    Tod
            
  5. just make sure that the bean method and the interface contain your customexceptions in their definition.
    thanks...sanjib
  6. I'm really sorry for my mistaken. Tinou is right.
  7. I'd suggest subclassing MySuperException from RemoteException.
  8. Did you post your question at the WebLogic EJB newsgroup? Sounds like a WLS 5.1 bug but I really cannot believe BEA left such a big hole in their flagship product.