• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

2. Which of the following circumstances should throw Exceptions?

 
Phoebe Song
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a.Someone tries to set the capacity of a PackingBox to a negative value.
b.A syntax error is found in a configuration file that an object uses to set its initial state.
c.A method that searches for a programmer-specified word in a string array cannot find any occurrence of the word.


I believe is a, right?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Phoebe Song:
...I believe is a, right?

I think there are more graceful ways to handle user input error, so I'm going to say not A.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I would throw an exception for B and quite possibly A as well. It depends mostly on whether "someone" in A is a user or a programmer. I'd definitely throw an exception if it's a programmer. Even if it's a user, I might throw an exception from one method, and catch it in another. C is possible too, but unlikely unless there us some reason to expect that the array must contain the string in order for the program to function correctly. I'm not usually interested in being "graceful" - I want to make errors easy to detect, then fix them. But really, there is no one absolute answer here. I think we could probably imagine circumstances for each of these where you could argue either way. But in my experience, for most circumstances that I can imagine, I would throw an exception for B, maybe A, but probably not C.
[ April 11, 2008: Message edited by: Jim Yingst ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes sense. I'm still leaning towards "not" on A, but like you said, it depends. I could be talked into it.
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that as a user I'd be pretty miffed if an Exception got thrown on A.

It's as though accidentally trying to use my library card in the ATM machine causes a hole to open up under me, dropping me into a dark box which suddenly spits me out a few blocks away. I wouldn't know how to recover. Sheesh, all I wanted was 20 bucks that are mine in the first place.

But if "someone" is a programmer, and a negative value in the PackingBox might cause subtle bugs in my Crate class, potentially leading to a weird error where the freight ship might sink if it encountered a hurricane during a full moon (and only if whales are singing) I'd be pretty relieved if an exception was thrown. Lives could be saved!

Then again, if my PackingBoxes are just for the move across town and a negative value would just cause one of the PackingBoxes to be empty I might notify the user/programmer and they could deal with it or not as they pleased. An extra box, or an extra trip across town. Not such a huge deal.

I guess one question would be "is the user going to be around to deal with the problem"? With the configuration file in B if you try ignoring it or sending friendly messages to the user, you are likely to get some pretty crazy behavior (if any).
 
ali acbar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone fill me in on what packingbox refers to in terms of Java code?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ali acbar wrote:Can someone fill me in on what packingbox refers to in terms of Java code?


It's just the name of an imaginary class, made up for this example.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic