• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Propagating and Catching an exception problem

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

SCJP1.6 JAVA 6 CERTIFICATION Guide-310-065
by Katherine Sierra (Author), Bert Bates (Author)
exerCise 5-3




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()-1;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.
------



Is this solution correct ?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. The indentation makes the code very hard to understand. Try to indent your code properly.
2. The part which checks the length of the String and throws an exception should be in the reverse method not the main method. Then you have to call reverse method in a try-catch block (the try catch block will also have a finally block)...
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@mohitkumar

Ankit is correct
In your code the thrown Exception caught by main method that is not propogated.
 
reply
    Bookmark Topic Watch Topic
  • New Topic