wood burning stoves 2.0*
The moose likes Web Services Certification (SCDJWS/OCEJWSD) and the fly likes handling service-specific exceptions in client application (Ivan notes p. 396) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Services Certification (SCDJWS/OCEJWSD)
Bookmark "handling service-specific exceptions in client application (Ivan notes p. 396)" Watch "handling service-specific exceptions in client application (Ivan notes p. 396)" New topic
Author

handling service-specific exceptions in client application (Ivan notes p. 396)

Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 722
In Ivan's notes, p.396, there is a quote
"Service specific exceptions are all checked exceptions, with the exception of java.rmi.RemoteException and all subclasses of RemoteException. Service-specific exception thrown by a web service method are to be mapped to <wsdl:fault> elements declared in the abstract interface of the web service. A mapped Java excepiton class must be annotated with the @WebFault exeception."

The possible way of handling service-specific exception:
"Wrap the checked service-specific exceptions and throw an unchecked exception"

My question is in Java checked exceptions and unchecked exceptions (RuntimeException) are derived from different base classes. How can we convert a checked exception into an unchecked exception?
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1630
    
  23

Himai Minh wrote:
My question is in Java checked exceptions and unchecked exceptions (RuntimeException) are derived from different base classes. How can we convert a checked exception into an unchecked exception?

Why would you want to do that?
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 722
I just google "wrap checked exception in unchecked exception".
It may be something similar to this on the client side:

But why should we wrap the checked exception in RuntimeException?
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1076
    
    1

But why should we wrap the checked exception in RuntimeException?

converting checked exception into unchecked exception depends on business scenario.
Normally such conversion code should not occur .. exception should be clearly defined and categorized during design time of app.So always avoid this conversion at the first place.
But there are scenarios where in we require this conversion. For example -
consider an Stateless EJB which refer to an external Web Service implementation through @WebServiceRef annotation. This external web service is accessed through well defined WSDL file. This web service defines SomeFaultException in WSDL for its operation. Now in EJB method, we call external Web Service method to execute some business logic. Now let us say that web service returned checked exception [ means SomeFaultException (defined in WSDL)] back to EJB code. In this situation, we have to 3 options to handle this situation in EJB code

Option1


Option 2


Option 3


In Option 1, we are silently suppressing SomeFaultException and just printing stack trace on console. In this case, further code shall continue to run without even knowing that web service call has failed.

In Option 2, we are NOT suppressing SomeFaultException and wrapping it in Runtime Exception so that execution can be halted and we are preserving original SomeFaultException details by passing this exception in Runtime exception constructor.

In Option 3, we are propagating SomeFaultException to the method which calls this EJB method. This practice is normally not good as we are propagating/exposing web service exception into other layers of application. Normally, in application, only exceptions related to that application should be thrown and propagated.

Which option a developer choose depends on Business scenario and requirements.

Hope this explanation help !!

happy learning
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1630
    
  23

Himai Minh wrote:I just google "wrap checked exception in unchecked exception".
But why should we wrap the checked exception in RuntimeException?

Aha you meant 'wrapping" a checked exception, I didn't understand "converting" in your question.

I agree with Abhay and have a simple example as well. Let's say you have a Servlet-client of a Web Service (in the doGet() method you invoke the Web Service) and your Web Service is throwing a checked exception. You can't throw it to the client in that case because the method signature does not allow that:

In that case you have to throw a ServletException to the client.

Regards,
Frits
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: handling service-specific exceptions in client application (Ivan notes p. 396)
 
Similar Threads
Java Handeling Exceptions
Checke Unchecked exception
Exceptions
Exception hierarchy
Conversion of service specific exceptions to Runtime exceptions in WS.