Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exception program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "exception program" Watch "exception program" New topic

exception program

pooja jain
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
can any body help me to understand the program of K&B book on page 243 . The program is saying to make a method that will take a string & return reverse & also if we will not supply any string ( lenght() == 0 ) then it will throw a new exception .

I tried this much :

thanks in advance .
pooja .

Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
You did not declared in your code snippet the clas MyException. If your class you exception is a subclass of RuntimeException there will no be any problem, because Runtime exceptions are unchecked. However if MyException is subclass of MyException, but not subclass of RuntimeException you will have either to handle exception in your reverse method or throw it by means of the throws clause up to your main method, where you will have to handle it or add, once again, the trhows clause.

Sample #1: java.lang.RuntimeException (unchecked exception)

Sample #2: With java.lang.Exception (checked exception)

Be careful, because you named your class Exception and that might cause name conflicts. You have to use fully qualified names to refer to the java.lang classes in this case.
I agree. Here's the link:
subject: exception program
It's not a secret anymore!