• 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

K&B - Exercise - try/catch problem

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to write some code to K&B exercise 5-3.

The step is:

- Create a class & main

-Create a method called reverse to reverse the order of the characters in a String

-In reverse, if the length of the input String is 0, throw an exception.

-Try to call this method in main() and deal with the exception and include a finally statement.

My code is:



But the output is:


Why if statemnt is false but seems like Exception is still throwed??!!
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The problem is it is not throwing exception when checking ss.length, it is throwing exception in the loop, coz the first value is ss.length ie. out of range of string array coz the length is always 1 greater than the range of array.


This will remove the error and give the output as expected
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that case you got ArrayIndexOutOfBoundsException.
 
Maggie Zhang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for the reply,

But I still don't get it, the value in the for loop begins with ss.length() - 1, not length()....so how can it be out of range?
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that case you got ArrayIndexOutOfBoundsException. - it was to your question.

When we write that:
length()-1 -everything is OK
 
Maggie Zhang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Awe...I thought the code in the first reply was written by myself...thanks everyone!~~
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another important detail we should take into account is the following statement from K&B 6, page 363:
An exception that's never caught will cause your application to stop running.

I've modified the code as follows:



Due to the fact that Exception is propagated until the bottom of the call stack (in other words: never caught), the System.out.println("program has completed correctly") in line 6 is never reached - opposed to the scenario where Exception is caught at the right place (try-catch-block around line 5).

That's the output after running this piece of code:

Exception in thread "main" java.lang.Exception
at Propagate.reverse(Propagate.java:12)
at Propagate.main(Propagate.java:5)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic