This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Exception cast in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exception cast in Java" Watch "Exception cast in Java" New topic
Author

Exception cast in Java

Manikandan Swaminathan
Ranch Hand

Joined: Aug 10, 2008
Posts: 117
In a simple web application(No Struts or Spring), lets say i am getting an exception in the business layer and i want that to be sent to the UI layer for some reasons.
How can this be acheived? I have heard something like exception cast in Java. I googled for this,but i was not able to get the required informaton.
Can anyone please let me know the details?

Cheers
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4367
    
    8

I'm not sure what you mean by exception cast. The easiest way to get an exception from the business layer to the UI is just to throw it (or not catch it) - it will bubble up until you handle it, and if you only handle it in the UI that's where it will end up.

One thing that's quite common (and that might be what you're looking for) is when you want the business layer to throw a particular special type of exception so the UI doesn't have to know about the internal workings. Then you can catch a general exception, wrap it in your special one, and throw that. The pattern looks something like this:

 
Don't get me started about those stupid light bulbs.
 
subject: Exception cast in Java