aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B - Exercise - try/catch problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B - Exercise - try/catch problem" Watch "K&B - Exercise - try/catch problem" New topic
Author

K&B - Exercise - try/catch problem

Maggie Zhang
Greenhorn

Joined: Jun 13, 2009
Posts: 6
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

Joined: Jun 28, 2009
Posts: 174
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

Joined: Apr 20, 2009
Posts: 804
    
    1

In that case you got ArrayIndexOutOfBoundsException.


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Maggie Zhang
Greenhorn

Joined: Jun 13, 2009
Posts: 6

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

Joined: Apr 20, 2009
Posts: 804
    
    1

In that case you got ArrayIndexOutOfBoundsException. - it was to your question.

When we write that:
length()-1 -everything is OK
Maggie Zhang
Greenhorn

Joined: Jun 13, 2009
Posts: 6

Awe...I thought the code in the first reply was written by myself...thanks everyone!~~
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
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)


SCJP 6 (88%), SCWCD (89%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B - Exercise - try/catch problem