• 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

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
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("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:
I do some of my very best work in water. Like this tiny ad:
global solutions you can do at home or in your backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!