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

Welcome to IWETHEY!

New A Further Question on the Code Example
Thanks Dan for your well reasoned discussion. I'm not sure I agree with everything you said, but I'm going to take some time to put my thoughts in order on the topic.

However, I would like to explore the example code a bit. I started this thread with the intention of talking about catching explicit exceptions contrasted with catching general exceptions. Checked vs Unchecked, although related, is a bit of a side track.

Consider code snippet you shared...

try\n{\n...get data from user or whatever.\ncallBusinessLayerA(...);\ncallBusinessLayerB(...);\n...use data from business layer to setup view or whatever.\n}\ncatch ( ApplicationException e )\n{\n..set up error messages to display error from business layer...\n}
I assume the purpose of the catch clause is to catch the non-fatal failure modes of the try block and to report those failures in user in some fashion (e.g. log them, or an error dialog).

If that is the case, why wouldn't you catch Exceptions rather than ApplicationException. After all, failing because of a null pointer is just as much of a failure as anything else and I would expect all failures to be handled consistently.

I understand catching a specific exception in order to provide specific logic for a certain kind of failure, but ApplicationException seems to be too general for that purpose.

Thanks for the time and effort you have spent on this discussion.
--
-- Jim Weirich jweirich@one.net [link|http://onestepback.org|http://onestepback.org]
---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
Expand Edited by JimWeirich March 2, 2004, 01:20:53 PM EST
New Re: A Further Question on the Code Example
Well, I appologize for the side track. To be honest, part of the reason for even bringing up the isue was that there has been a lot of debate about it, but most of the complaints against were based upon developers misusing it, which I thought wasn't necessarily a good reason for complaint (more of an educational opportunity). I don't really care to convince anyone, as everyone's experience is a little different, and situational uses tend to be unprovable and uninteresting (from a pure "good" versus "bad" perspective).

But, to answer your real question, let me complete that example with an explanation. The reason why I wouldn't catch a more general exception (java.lang.Exception) is that it's caught somewhere else (and not by my code). I think there is a difference between the two, even to the user.

First, I may not necessarily need to do anything other than display a pretty message to the user for an application exception (now, I'm going to go off the deep end here and get myself into trouble, but it's even interesting, to me, to have validation checks throw exceptions that get displayed to the user through that method). It may not even log (in some cases, it should...as usual, depending). And there's certainly no need for a full stack trace in the log.

For the more general exception (meaning, an unchecked exception or such, like java.lang.Exception), the container (in the example, that would be the web container) would ultimately catch the exception, log to a specific error log, dump the stack trace, etc. It would also display a different kind of error message. Why? First, because it really is a different kind of error. If it's a null pointer exception, there's no pretty way to tell the user that they're staring at a bug. Instead, you display a big general "Yikes!" message and send the user somewhere, as opposed to redisplaying the same page with a couple of error messages or something. I know this may sound stupid, but you want the user to complain about those big ones so that they will get fixed (this is the old issue of: I don't have time to fix it, but if the user screams loud enough, my manager will make the time).

Anyway, if you, ultimately, consider them to be the same basic thing, then, yes, you can catch the more general exception and the checked exceptions will probably get in the way.

Dan
     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)

I'll teach you to be happy! I'll teach your grandmother to suck eggs!
117 ms