• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exercise Problem...

 
sweety singh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody tell.. what are we supposed to do in this code... i am slightly confused....

Exercise 5-3: Propagating and Catching an Exception


In this exercise you're going to create two methods that deal with exceptions. One of the methods is the main() method, which will call another method. If an exception is thrown in the other method, main() must deal with it. A finally statement will be included to indicate that the program has completed. The method that main() will call will be named reverse, and it will reverse the order of the characters in a String. If the String contains no characters, reverse will propagate an exception up to the main() method.

Create a class called Propagate and a main() method, which will remain empty for now.

Create a method called reverse. It takes an argument of a string and returns a String.

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

Now include the code to reverse the order of the String. Because this isn't the main topic of this chapter, the reversal code has been provided, but feel free to try it on your own.

String reverseStr = "";
for(int i=s. length()-l;i>=0;--i) {
reverseStr += s.charAt(i);
}
return reverseStr;

Now in the main() method you will attempt to call this method and deal with any potential exceptions. Additionally, you will include a finally statement that displays when main() has finished.
 
Lave Kulshreshtha
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your doubt?

-Lave
 
sweety singh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where are we supposed to write the filly statement.... and what about the propagate method..
 
sweety singh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i mean finaly statement... sorry..
 
Lave Kulshreshtha
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look you are calling a method for e.g. reverse() in main. Now if reverse() throws a checked exception then main should handle this exception but if reverse() throws an unchecked excption then main may or may not handle it.
Do one thing write following code in main()
try{
reverse();
} catch (Exception exception) {
exception.printStackTrace(); /* put some extra code if you need */
} finally {
/* Your code */

}

Lave
 
sweety singh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do i need to mention the catch ststement...???
 
Lave Kulshreshtha
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you re using finally, then may skip using catch.
With try statement you can either use
1). Only catch
2). Only finally
3). Both catch and finally

-Lave
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lave is correct, but I would suggest you declare that your reverse method throws an unchecked Exception; StringArrayOutOfBoundsException is the most likely. Then rather than your main method catching Exception catch StringArrayOutOfBoundsException. Far better than simply catching Exception.

I think those Exceptions are all in java.lang, so you don't need any import statements. Lots more about Exceptions in the Java Tutorial.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry, but I spelt the name of the Exception wrongly. It is StringIndexOutOfBoundsException. The "for" loop is correctly formed and will not create a condition which throws an Exception; you will have to throw the Exception yourself.
 
sweety singh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. It would be nice to see what you have got working, please.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic