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.


           +------ 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 {

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

    } catch(MyException2 e) {

    } catch(MyException3 e) {

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

    } 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")) {

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

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

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

        } else {

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


  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.

  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?

  5. just make sure that the bean method and the interface contain your customexceptions in their definition.
  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.