wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Exercise Problem... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exercise Problem..." Watch "Exercise Problem..." New topic
Author

Exercise Problem...

sweety singh
Ranch Hand

Joined: Dec 15, 2007
Posts: 49
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

Joined: Oct 21, 2007
Posts: 106
What is your doubt?

-Lave


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, ITIL V3 Foundation Certification
sweety singh
Ranch Hand

Joined: Dec 15, 2007
Posts: 49
where are we supposed to write the filly statement.... and what about the propagate method..
sweety singh
Ranch Hand

Joined: Dec 15, 2007
Posts: 49
i mean finaly statement... sorry..
Lave Kulshreshtha
Ranch Hand

Joined: Oct 21, 2007
Posts: 106
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

Joined: Dec 15, 2007
Posts: 49
do i need to mention the catch ststement...???
Lave Kulshreshtha
Ranch Hand

Joined: Oct 21, 2007
Posts: 106
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

Joined: Oct 13, 2005
Posts: 37907
    
  22
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

Joined: Oct 13, 2005
Posts: 37907
    
  22
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

Joined: Dec 15, 2007
Posts: 49
thank you...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
You're welcome. It would be nice to see what you have got working, please.
 
Consider Paul's rocket mass heater.
 
subject: Exercise Problem...
 
Similar Threads
about exception
Propagating and Catching an exception problem
K&B - Exercise - try/catch problem
how to do this? (exception) can u help me to fix?
Exercise on Propagating Exceptions. Am I missing something?