• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exercise on Propagating Exceptions. Am I missing something?

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marcus Jastrebowski
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic