File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes 2.	 Which of the following circumstances should throw Exceptions?  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "2.	 Which of the following circumstances should throw Exceptions?  " Watch "2.	 Which of the following circumstances should throw Exceptions?  " New topic
Author

2. Which of the following circumstances should throw Exceptions?

Phoebe Song
Ranch Hand

Joined: Jan 17, 2002
Posts: 54
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Nov 03, 2006
Posts: 1344
    
  12
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

Joined: Oct 16, 2009
Posts: 1
Can someone fill me in on what packingbox refers to in terms of Java code?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
And welcome to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 2. Which of the following circumstances should throw Exceptions?
 
Similar Threads
process special Word quote chartacters in jsp
Fixing the size of the td in html
Your most hated word
WA #1.....word association
prepareStatement Help