This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exercise on Propagating Exceptions. Am I missing something? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exercise on Propagating Exceptions. Am I missing something?" Watch "Exercise on Propagating Exceptions. Am I missing something?" New topic
Author

Exercise on Propagating Exceptions. Am I missing something?

Marcus Jastrebowski
Ranch Hand

Joined: Nov 15, 2007
Posts: 55
Perhaps I'm a dope. I ain't gonna dispute it. However, I do not understand the Exercise 5-3 "Propagating and Catching Exceptions" page 352-53 in Sierra/Bates.

The instruction says In reverse(), check if the String has a length of 0 by using String.length() method. If the length is 0, the reverse() method will throw exception.

It seems to me that this instruction can't ever be satisfied since the ArrayIndexOutOfBoundsException will always be thrown when the args[0] does not exist... Am I missing something fundamental here?

Here is the "natural" way I can think of solving this exercise... Any other ideas?

R van Vliet
Ranch Hand

Joined: Nov 10, 2007
Posts: 144
Hi Marcus,

Although in every practical sense you're correct it is actually possible to pass a string of length 0 to a Java program, resulting in args.length being 1 and args[0] being a string of length 0 :



In which case you can check the length of the string. Note that your reverse() will function correctly even if the string length is null, but I assume the instructions want you to throw an exception for a reason not related to an illegal argument.

Hope that helped
Marcus Jastrebowski
Ranch Hand

Joined: Nov 15, 2007
Posts: 55
Thanks R, and so, after a little bit of fiddling around, I was able to satisfy this requirement: "In reverse(), check if the String has a length of 0 by using String.length() method. If the length is 0, the reverse() method will throw exception." The code gets considerably longer that way, but I guess the idea of this particular exercise was to show how the exceptions are propagated up the stack from one method to another.

Here is the reworked version, and it seems to be working fine now.

Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Here is my shot at the exercise.


Since my code throws Exception [Checked] I need to declare it in the reverse method. But since you are throwing ArrayIndexOutOfBoundsException that inherits IndexOutOfBoundsException which inturn inherits from RunTimeException there is no need to declare it in the reverse() method signature.
THanks
Deepak


SCJP, SCWCD, SCBCD
Luca Romanello
Greenhorn

Joined: May 30, 2002
Posts: 11
Originally posted by Marcus Jastrebowski:
Thanks R, and so, after a little bit of fiddling around, I was able to satisfy this requirement: "In reverse(), check if the String has a length of 0 by using String.length() method. If the length is 0, the reverse() method will throw exception." The code gets considerably longer that way, but I guess the idea of this particular exercise was to show how the exceptions are propagated up the stack from one method to another.

Here is the reworked version, and it seems to be working fine now.



Hi Marcus
Please note that if you pass a String with length == 0 to your code, you will obtain an uncaught exception, since in main() you handle ArrayIndexOutOfBoundsException, but not IndexOutOfBoundsException (which, being a subclass of RuntimeException, hasn't to be declared in the throws class of method reverse() but, being a superclass of ArrayIndexOutOfBoundsException, isn't handled by the catches you wrote). The code works, though, passing no parameter. Is this correct (that is, is this what you really wanted )?
Hope this helps
Regards
LR
Marcus Jastrebowski
Ranch Hand

Joined: Nov 15, 2007
Posts: 55
Thank you very much Luca, Deepak for catching my typo. That's my problem unfortunately, I tend to type too fast, being careless -- which causes me to make silly mistakes -- which lends to losing points on the tests.

Definitely IndexOutOfBoundsException was incorrect. Should have been ArrayIndexOutOfBoundsException instead. The IndexOutOfBoundsException is a broader bucket though (the super of ArrayIndexOutOfBoundsException), so it was still working when args[0] was missing. However, I am not exactly clear why the compiler didn't complain when the superclass's ex object was passed on to the main method... Was it the polymorphism in action (?), I guess it must be.

The code below works properly when I run it. Thanks again.



[ November 23, 2007: Message edited by: Marcus Jastrebowski ]
[ November 23, 2007: Message edited by: Marcus Jastrebowski ]
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Marcus Jastrebowski:
However, I am not exactly clear why the compiler didn't complain when the superclass's ex object was passed on to the main method... Was it the polymorphism in action (?), I guess it must be.


Hi Marcus,
As Luca pointed out, the compiler does not complain because IndexOutOfBoundsException and ArrayIndexOutOfBoundsException are both subclasses of RuntimeException, which means they're unchecked. Your reverse() method therefore doesn't even need to explicitly declare that it throws ArrayIndexOutOfBoundsException.


SCJP 5.0
Marcus Jastrebowski
Ranch Hand

Joined: Nov 15, 2007
Posts: 55
Originally posted by Kelvin Lim:


Your reverse() method therefore doesn't even need to explicitly declare that it throws ArrayIndexOutOfBoundsException.


Hi Kevin, thanks for your input, and I do understand everybody's point. However, I thought that that was exactly the point of this particular exercise, as I wrote at the very top of this thread: to throw the exception from the reverse() method. Otherwise, the solution in my initial post seems to be what you gentlemen are saying? Am I still missing something?

Marcus
Craig Collins
Greenhorn

Joined: Mar 30, 2008
Posts: 8
Here is my attempt today, with improvements after reading a few threads here!

In reverse() I'm throwing an IllegalArgumentException ... propogating it to
main() ... catch it there and print the stack trace;



Running this with 1) a string 2) no argument and 3) an empty String "" produces;



cheers and good luck!
[ April 01, 2008: Message edited by: Craig Collins ]
 
Don't get me started about those stupid light bulbs.
 
subject: Exercise on Propagating Exceptions. Am I missing something?
 
Similar Threads
Propogating Exceptions - Exercise5.3 in K&B
Propagating and Catching an exception problem
K&B - Exercise - try/catch problem
how to do this? (exception) can u help me to fix?
missing return statement error message when compiling class