cancel
Showing results for 
Search instead for 
Did you mean: 

Exception handling in J2EE

Former Member
0 Kudos
198

Dear J2EE developers,

When there is a fault in my method in my Session Bean then I throw a EJBException (like this: throw new EJBException(“Error in method!”))

But when I call the method from a JSP and cause by purpose a fault then I get the next error:

Exception caught com.sap.engine.services.ejb.exceptions.BaseEJBException: Exception in method [methodName].

… more text …

Caused by: javax.ejb.EJBException: Error in method!

… more text …

When I call the method from a Webservice then I get the next error:

Exception in method [methodName].

My question is: How can I get my message “Error in method!” instead of the exception above or the long exception from my JSP. I really hope that this is possible. How can I otherwise get a decent exception message in my applications!

Best regards,

Marinus Geuze

Accepted Solutions (1)

Accepted Solutions (1)

SidBhattacharya
Product and Topic Expert
Product and Topic Expert
0 Kudos

You need to throw an application exception (ex. throw new Exception("Error in method!") ) instead of a system-level exception like EJBException.

Application exceptions are thrown back to the client.

Vlado
Advisor
Advisor
0 Kudos

Hi Siddhartha,

java.lang.Exception itself is not an application exception. Section 18.2.1 of the spec:

<i>An application exception class must be a subclass (direct or indirect) of java.lang.Exception. An application exception class must not be defined as a subclass of the java.lang.RuntimeException, or of the java.rmi.RemoteException. These are reserved for system exceptions. (See next subsection).</i>

Best regards,

Vladimir

SidBhattacharya
Product and Topic Expert
Product and Topic Expert
0 Kudos

throw new Exception("error") --> Example of how to throw an application exception.

your actual Exception class could be

public class UserException extends Exception

{

}

public void fooBar()throws UserException

{

}

you can do a throw new UserException() or throw new Exception()...Both will be app exceptions..Thats what I meant...

Vlado
Advisor
Advisor
0 Kudos

You <b>cannot</b>

throw new Exception()

if java.lang.Exception (or its parent java.lang.Throwable) is not declared in the throws clause of the method. And if it is declared it's not an application exception - the spec is clear on this. However,

throw new UserException()

is legal.

Answers (2)

Answers (2)

Former Member
0 Kudos

Dear helpers,

I thought that I had fixed the problem but it still exists. I implemented a own exception class (I used the example “Creating a J2EE-Based Car Rental Application”).

But when I test my web service (with the web service navigator) and cause a error by purpose then I get this as return:

-


An error has occurred. Maybe the request is not accepted by the server:

test.SAP_PersDefaultException

-


When I test the web service from the example, then I get the same result:

-


test.QuickCarRentalException

-


Only the name of the exception class is different. When I look in response then I see my error message. So I though it was working.

But when I use the web service in Web Dynpro, and cause again a error by purpose, web dynpro returns a empty error message.

I compared this behavior with the example “Using Web Dynpro to Avail of the Car Rental Web Service” and the behavior is exactly the same. So again a empty error message.

I am sure this is caused by the return test.SAP_PersDefaultException, this should be the error message defined in my session bean (and the one which is shown in the response).

I hope you can help me to solve this.

Thanks.

Vlado
Advisor
Advisor
0 Kudos

Hi Marinus,

javax.ejb.EJBException is a runtime exception and as such is treated as a system exception. If you throw instead an application exception, it won't be wrapped into another exception and the EJB client (in your case the JSP) will receive exactly that exception unchanged.

For more information on system and application exceptions please check chapter 18 of the EJB 2.0 spec.

Hope that helps,

Vladimir