This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B - Exercise - try/catch problem

 
Maggie Zhang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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??!!
 
Jacob Sonia
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • 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
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case you got ArrayIndexOutOfBoundsException.
 
Maggie Zhang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator

Awe...I thought the code in the first reply was written by myself...thanks everyone!~~
 
Ulrich Vormbrock
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • 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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic