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

Welcome to IWETHEY!

New What ends up happening ...
is that you have reams of code catching exceptions, wrapping them in some new exception and throwing them. What value have you added by wrapping the original exception 10 times? Take a typical example. Your low level data layer fails getting data from the database for some reason (e.g. because the database is down). In most cases there is nothing that you can really do with this exception except log the error and present an error message to the user. What value is there in making all the intermediate layers catch the exception wrap it in a different exception and throw the new exception? What value have you added? The bottom line is that the topmost gui layer should catch the exception and present the error message whether or not you wrapped the exception 10 times or not.
New Re: What ends up happening ...
I think 10 times is a little excessive. And I think the database access (specifically SQLException) is also a poor example, as it's debatable whether it should have been a checked exception in the first place.

Dan
New Re: What ends up happening ...
[i]I think the database access (specifically SQLException) is also a poor example, as it's debatable whether it should have been a checked exception in the first place.[/i]

OK but its debateable whether any exception should be a checked exception. For instance, InputStream - a fully abstract class - insists than any method may throw an IOException which must be caught (its checked). Yet there are instances of InputStream that do not throw this exception - yet the programmer is still burdened with writing code to handle exceptions that will not be thrown. This is a fundamental problem with static typing and hard wired method signatures.

InputStream in = new StringBufferInputStream(buffer);

int c = -1;
try { c = in.read(); } catch(IOException ex) {} // won't really throw

Of course, you can't exactly know that in is going to reference something that won't throw.

My biggest complaint though is that checked exceptions leak concerns of implementation into the interface. This results in very brittle systems.


The tree of research must from time to time be refreshed with the blood of bean counters.
     -- Alan Kay
New You didn't answer the question ...
What value is there in wrapping exceptions?

10 was obviously an exaggeration.

What do you think should have been an unchecked exception?
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)

One thing you ought to know: well, I am the Mae-stro!
79 ms