• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic