This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

how to do this? (exception) can u help me to fix?  RSS feed

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
question:
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.

n Create a class called Propagate and a main() method, which will remain empty for now.
n Create a method called reverse. It takes an argument of a String and returns
a String.
n 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 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.

==========================================================my code:
class propagate {
public static void main (String args[]){
try {
System.out.println (reverse("hello this is adam"));
}
catch (Exception ex){
System.out.println("error");
}
finally{
System.out.println("program exits");

}
}


public static String reverse (String str){
String reverseStr = "";
if (str.length()== 0) {
}
else {
for(int i=str.length() -1;i>=0;--i){
reverseStr += str.charAt(i);
}
}
return reverseStr;
}
}


=====================
where exactly should i handle the exception ?
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you should declare reverse() throws an exception and when a special condition happen which is length() is 0 it throw the exception.
you can modify the reverse() as follows:
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!