This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
Bartender
Posts: 1925
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Creator of Enthuware JWS+ V6
Posts: 3348
303
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Bartender
Posts: 1925
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3348
303
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic