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 One phrase says it all.
"It's more information (even if it's the wrong info)."

I don't think the ability to present more info compensates for the fact that the information can be wrong, there is no way to make it right, and you are forced to act on that information (wrongly).

Could you offer an example of _correct_ use of checked exceptions? So far, people have been agreeing on how bad each offered example was.
--

The number of the beast - vi vi vi
--[link|http://c2.com/cgi/wiki?QuotesOnComputers|Delexa Jones]
New Caught my eye too
I seem to recall that the technical term for valueless information is NOISE!


The tree of research must from time to time be refreshed with the blood of bean counters.
     -- Alan Kay
New Re: One phrase says it all.
As I was writing that phrase about more information being good, regardless of it being wrong kind of struck me as a really poor way of explaining things. On the surface, I would completely agree with you.

It makes some sense that, given an interface (with defined checked exceptions) with a particular implementation not throwing said exceptions, that the interface is considered "wrong" because an implementation doesn't actually implement it (that is, it doesn't have the possibility of throwing an exception, so it could be said that the interface isn't truly implemented properly....it's "wrong").

However, that's not how I looked at it (though that appears to be how I said it, which is obviously my fault). The way I looked at it was that the interface defines a particular set of signatures (for our purposes, one is enough). For that particular signature, it defines three basic things: the return type, the parameters, and a checked exception (I know, there are other things in there, such as the ordering and types of parameters, etc...just trying to keep things simple). The checked exception means (as I look at it) that the method *may* throw an exception of that type. That means that it may not. In fact, I *hope* that it doesn't, but I code such that it might.

Because I trust the signature (the compiler says I can), I assume that it's possible. The checked exception gives me additional information in that it's a *hint* that a particular exception *may* (and *can*) be thrown. It doesn't state that it *must* or that it *will* under any given circumstances. In that case, it's additional information that isn't necessarily *wrong* if a particular implementation doesn't actually throw the said exception. It comes back to perception, I guess.

If we change the example so that it was an unchecked exception, instead, would that change anything? First off, I would *hope* and assume that the interface was well enough documented that it would indicate that an unchecked exception might be thrown under certain situations (something like: this method may throw this exception....). If not, then I would not have a lot of trust in that interface as I'd probably get exceptions thrown and not know that I was to catch them (this isn't a question of *where*, but the need to know that it can so that I can make the decision as to where).

Given that it's well documented, what about an implementation of that interface that didn't throw the exception? Depending upon how I use that interface, I may not know that a particular implementation doesn't throw the exception? Why? Because the construction of the implementation may be hidden from me, so I may not even know what class I'm actually using (that's the whole point of using an interface, after all). So, I would handle the situation identically, regardless of whether there was a checked or unchecked exception (granted that there may be slight differences in the syntax, since I have more flexibility as to where I deal with the unchecked exception, but I also have better inherent documentation with the checked one...it's a trade-off, to a certain extent).

As for a good example of a checked exception...

Since most of my current experience is with web-based apps, I'll use a fairly simple example: a Struts-based web app.

I create a business object layer that handles the business logic (and, somewhere in there, persistence). That layer defines some interfaces and concrete classes that the presentation layer interfaces with. Where appropriate, the business layer throws a standard application exception (with possibly a couple sub-classes if necessary). In the presentation layer (say, the struts action class), calls are made into the business layer. Since I, as the business layer writer, provide a single exception layer to the presentation layer, it can easily have code that does something like this:

try
{
...get data from user or whatever.
callBusinessLayerA(...);
callBusinessLayerB(...);
...use data from business layer to setup view or whatever.
}
catch ( ApplicationException e )
{
..set up error messages to display error from business layer...
}

Obviously, I'm leaving out lots of details, but the idea is that the presentation layer only deals with one (or only a very small number) application layer.

Of course, this can just as easily be done using unchecked exceptions. The difference is one of safety. As the writer of the business layer, I'm forcing the presentation layer developer to deal with my exceptions. If he merely ignores them (nothing in the catch), then hopefully, that'll get caught in testing or code review. In fact, it's fairly easy to write up a tool to automatically find those kinds of bugs (there are probably tools out there that do it, in fact).

I realize that it's a fairly simple example, but the idea of layering the exceptions simplifies things considerably. When dealing with junior to mid-level developers, I like the additional safety (try working at places like EDS or Accenture where the developers are not trained or taught...having extra checks like this are really helpful).

Dan
New I remain unconvinced
And its entirely based on my experience.

The fact is that the vast majority of busy developers don't have or don't take the time to follow your wrapping strategy. Its much too time inefficient and the payoff isn't really there (practically nobody on the user side of the system sees the benefit).

(There is a whole other rant I'm holding back on the excessive "wrapping" of layers - ie JDBC is already an abstraction layer - don't hide it).

Which tells me that its a failed "solution". I myself have become annoyed during a time crunch at changing the implementation of a method only to have a new checked exception appear in the implementation. The need to suddenly change the method signatures of every method on the call stack of said method (or take time out and write a wrapper exception class) annoyed me to the point that I simply swallowed it. Sue me I was on a time crunch. I've done this more than once BTW.

More importantly, this is rampant in code I see elsewhere. Which tells me that we haven't really solved the problem. More importantly, were these exceptions not checked and the burden of changing all the method signatures not there, a catch clause would likely simply be put at a reasonably sensible default location (like at the event polling/web request handling bottleneck) and its resolution could be incrementally improved based on the kinds of errors found in testing.





The tree of research must from time to time be refreshed with the blood of bean counters.
     -- Alan Kay
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
New Bad for class libraries?
Seems that all the counterexamples come from shared class libraries and involve inheritance and implementing of interfaces. Your example is from the code fully under your control and (I am speculating) does not use deep inheritance/implementation hierarchy.

Could that be the distinction between correct and incorrect use of checked exception? And, if so, don't checked exception make code less easy to reuse and refactor?
--

The number of the beast - vi vi vi
--[link|http://c2.com/cgi/wiki?QuotesOnComputers|Delexa Jones]
     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)

Lather, rinse, repeat.
142 ms