Re: Exception Names
- From: Wojtek <nowhere@xxxxx>
- Date: Thu, 02 Apr 2009 08:16:00 -0700
Arved Sandstrom wrote :
Tom Anderson wrote:On Tue, 31 Mar 2009, Lew wrote:
Lew wrote:Besides, there's nothing exceptional about reaching the end of a stream. One would venture to say that one nearly always reaches the end of a stream, *unless* something exceptional happens.
Tom Anderson wrote:I'd say that getting end of stream is a highly exceptional event for a read method. The vast majority of the time, you get a byte back!
Nice little bit of rhetorical spin there.
It's not spin, it's a different perspective.
You define majority relative to the number of read events, where I was referring to the number of streams.
Yes, exactly.
I'm going to stand by the notion that most of the time *one uses a stream* one reads the whole thing. Your statement that most of the reads do not hit the end of the stream is trivially correct, but that still doesn't make reading a stream all the way through an exceptional occurrence.
Okay, fair enough.
I also think that whether an event is exceptional has at best a marginal connection to whether it's a good idea to use an exception for it. I think that's an overly literal and dogmatic approach to take.
tom
Generally I see little rhyme or reason as to why some occurrences in Java are handled with return values and others with exceptions. For example, there's nothing exceptional about not finding a file or trying to write to a read-only file, and yet we've got FileNotFoundException.
IOW, exceptions in Java are typically used for _expected_ behaviour. IMHO rather than discussing what exceptions to add, we ought to be discussing how many exceptions that aren't exceptions we can get rid of. An exception should be for a situation that causes a break away from normal program flow...and handling end of stream doesn't fit that bill.
In case I wasn't clear (:-)), I'm arguing for _more_ methods with return values, not less.
I would rather use an exception than a return value for some things. Like a file that cannot be found.
Otherwise I might need to pass a value back through many methods, back to where my application can handle the problem.
Setting up a try/catch high up, then calling a method, which calls a method, ..., which tries to open the file. If there is a problem I do not need to make a test at each method to see if I can continue processing, or I need to return right away. It can become VERY cumbersome passing back a false (or whatever).
And there may be other problems that appear also. Maybe the file open is OK, but the disk is almost full. Maybe the stupid user pulled out the USB drive, and so on.
Using an exception I can handle the problem at a point in the application which was designed to recover gracefully.
--
Wojtek :-)
.
- Follow-Ups:
- Re: Exception Names
- From: Arved Sandstrom
- Re: Exception Names
- References:
- Re: Exception Names
- From: Tom Anderson
- Re: Exception Names
- From: Lew
- Re: Exception Names
- From: Tom Anderson
- Re: Exception Names
- From: Arved Sandstrom
- Re: Exception Names
- Prev by Date: Re: Exception Names
- Next by Date: Re: encryption
- Previous by thread: Re: Exception Names
- Next by thread: Re: Exception Names
- Index(es):
Relevant Pages
|