Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to throw a RunTimeException from a Servlet Consuming and Displaying XML

 
swathi bairu
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a running RESTEasy service that is getting called from a servlet. The output of the service is XML. When a RuntimeException is thrown from this servlet, I see a browser with just an empty page. I want the exception to be displayed on the browser. Please suggest.

My Servlet

 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really shouldn't just throw Exceptions from user code into the server. The server may not do what you want, and it's really not under any obligation to do so. Generally, you'll get some sort of "503" error, though.

A better solution would be to construct an XML string with an error message in it and return that in place of the XML that you couldn't construct.
 
swathi bairu
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:You really shouldn't just throw Exceptions from user code into the server. The server may not do what you want, and it's really not under any obligation to do so. Generally, you'll get some sort of "503" error, though.

A better solution would be to construct an XML string with an error message in it and return that in place of the XML that you couldn't construct.


I changed

throw new RuntimeException("Failed request with HTTP status: "+response.getStatus());

to

pw.println("<ExceptionOccured>"+"Failed request with HTTP status: "+response.getStatus()+"</ExceptionOccured>");

Please let me know if this is the right way to display an error on browser.
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a start.

Most such things tend to look more like this:


Just as a typical example. Or sometimes, the "error" element is a sub-element of the normally-expected XML output. But regardless, something complex enough that you'd probably want to invoke a separate method to generate it all.

Note that XML isn't normally indended for people to admire - it's more likely to be used to facilitate web services, so the client wouldn't typically be a browser, it would be some sort of application. In which case, that application would need to know how to detect and handle such responses.
 
swathi bairu
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:It's a start.

Most such things tend to look more like this:


Just as a typical example. Or sometimes, the "error" element is a sub-element of the normally-expected XML output. But regardless, something complex enough that you'd probably want to invoke a separate method to generate it all.

Note that XML isn't normally indended for people to admire - it's more likely to be used to facilitate web services, so the client wouldn't typically be a browser, it would be some sort of application. In which case, that application would need to know how to detect and handle such responses.


That helps. Thank you @Tim Holloway
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic