IWETHEY v. 0.3.0 | TODO
1,095 registered users | 1 active user | 1 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Re: You didn't answer the question ...
Wrapping exception is used to create a facade to the next layer. So, since SQL exceptions are checked, I can do one of two things: I can wrap them and rethrow them as application-specific checked exceptions, or I can rethrow them as unchecked exceptions. For SQL exceptions, I've seen both. Personally, I usually wrap them in checked exceptions because I like to associate a prettier error message with them as close to where it happens (I have more contextual information at that point). Then, I have a simple application-specific checked exception that the higher layer (say the presentation layer) can deal with in a generic, simple fashion (in some cases, the higher layer may not even need to know anything about the exception because it contains enough information for display purposes, but that's dependent upon the application type and how it's designed).

I approach unchecked exceptions in this way: if it's a programmer's error (i.e. a null pointer exception or something like that), an error (i.e. out of memory), or something that can't be handled and dealt with at a lower level (because the contextual info isn't helpful, or it doesn't make sense to), then using unchecked exceptions works well. However, if it's a application exception, such as bad input from the user, database going down (and this one can go either way...it depends upon the interface to the user), etc. then checked exceptions are nice because they can usually be handled nicely by the upper layer.

Hope that answers your question.

Dan
New A number of comments
1. in any given layer the exception can occur far down the call stack. Especially if you write short methods you may be far down the call stack when you catch and wrap an exception. This means that every method above you in the call stack needs to do something with the exception, probably just throw it, clearly a waste.
2. If each layer only throws 1 exception you lose information. The UI may want to display different pages depending on the exception. For example, if you receive a licensing exception the UI may want to display a licensing page, while a validation exception may redisplay the form. By wrapping the exceptions in a general exception youy make it hard for the client to figure out what really went wrong. The client then has to go and look at the wrapped exception and get the cause.
3. Many times you have a number of layers and they just wrap the exception without adding any new information.
     Exception Handling Policy - (JimWeirich) - (48)
         Possibly it depends on what level of code you are writing... - (Simon_Jester) - (38)
             Ditto. - (admin) - (37)
                 Because checked exceptions are STOOPID! - (tuberculosis) - (35)
                     One reason I like Spring Framework - (admin) - (1)
                         Absolutely - (bluke)
                     Re: Because checked exceptions are STOOPID! - (dshellman) - (23)
                         One respectable reason - (admin) - (13)
                             Re: One respectable reason - (dshellman) - (12)
                                 The problem with Java model is that I cannot - (Arkadiy) - (1)
                                     Re: The problem with Java model is that I cannot - (dshellman)
                                 But that would (i.e, *does*) defeat the whole purpose! - (CRConrad) - (3)
                                     ICLRPD (new thread) - (drewk)
                                     Re: But that would (i.e, *does*) defeat the whole purpose! - (dshellman) - (1)
                                         Sorry, either I don't get your meaning, or you didn't mine. - (CRConrad)
                                 What ends up happening ... - (bluke) - (5)
                                     Re: What ends up happening ... - (dshellman) - (4)
                                         Re: What ends up happening ... - (tuberculosis)
                                         You didn't answer the question ... - (bluke) - (2)
                                             Re: You didn't answer the question ... - (dshellman) - (1)
                                                 A number of comments - (bluke)
                         We already discussed this - (ben_tilly) - (8)
                             Re: We already discussed this - (dshellman) - (7)
                                 Blame the tool - (tuberculosis)
                                 You're right, that is unfortunate - (ben_tilly) - (5)
                                     Re: You're right, that is unfortunate - (dshellman) - (4)
                                         You have to balance benefit/cost - (ben_tilly) - (3)
                                             s/Knuth/Dijkstra/ - (a6l6e6x) - (2)
                                                 I had it right the first time - (ben_tilly) - (1)
                                                     I stand corrected then. - (a6l6e6x)
                     Re: Because checked exceptions are STOOPID! - (dshellman) - (8)
                         Hmm... - (CRConrad)
                         One phrase says it all. - (Arkadiy) - (6)
                             Caught my eye too - (tuberculosis)
                             Re: One phrase says it all. - (dshellman) - (4)
                                 I remain unconvinced - (tuberculosis)
                                 A Further Question on the Code Example - (JimWeirich) - (1)
                                     Re: A Further Question on the Code Example - (dshellman)
                                 Bad for class libraries? - (Arkadiy)
                 Re: Ditto. - (JimWeirich)
         I meant Exception the class - (bluke) - (7)
             Don't catch null pointer exceptions? - (JimWeirich) - (6)
                 In development yes - (bluke) - (5)
                     Re: In development yes - (JimWeirich) - (4)
                         Agreed - (bluke) - (3)
                             So wouldn't the remedy be... - (CRConrad) - (2)
                                 cf. Spring Framework :-) -NT - (admin)
                                 I agree ... - (bluke)
         Another reason to catch specifics - (jb4)

Thine is a mere venal sin which may be atoned: dast not move it very far or very often, or Ever! -- to the 7-11 for a Big G{ulp..} and a Lottery ticket. This be thy penance.
221 ms