permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about exception" Watch "about exception" New topic
Author

about exception

Nisha Pinjarkar
Greenhorn

Joined: Apr 01, 2007
Posts: 20
In Ex 5-3 of k&b, it says if string length is 0, it will throw an exception. Could someone please tell what exception will it throw..how to acomplish this?

Thanks.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Can you explain more about the question?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Nisha,

Could you please post the question, I dont find that 5.3.



Thanks and Regards,
cmbhatt


cmbhatt
Nisha Pinjarkar
Greenhorn

Joined: Apr 01, 2007
Posts: 20
THe ques is..
-create a class called Propagate with main() method
-create mth called reverse that takes a string arg
-if str.length is 0 this reverse method will throw an exception
-if str.length is > 0, write code to reverse the str.
-Now in main(), call the reverse() method, deal with any potential exceptions. add finally stmt that displays when main() has finished.

I wrote the following code..

public class Propagate
{

public static void main(String[] args) {

try{
String s2=reverse("nisha");
System.out.println("Reverse String ="+s2);
}
catch(Exception e){
System.out.println("exception caught in main()");
}
finally
{
System.out.println("main() ended");
}
}
public static String reverse(String s)
{
String s1 = s;
if(s1.length()==0){

//what exception to throw?
}
String revStr = "";
for(int i=s1.length()-1;i>=0;--i)
{
revStr +=s1.charAt(i);
}
return revStr;


}

}
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi Chu,

the exercise says that you should write a method, that takes a String as parameter (and returns a String).

The method's name should be "reverse". And the exercise demands:
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.

I think it is meant, that you should program this test for zero-length and if this condition is true, the method shall just throw an exception.

As K^B doesn't tell what exception, just throw any.
Just as a programming exercise.
Perhaps try checked or unchecked (e.g. runtime) exceptions to see the difference.


Yours,
Bu.


all events occur in real time
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
hi Nisha,



You can't throw Exception because it is checked exception and you must put the "throw new Exception()" in the try {} block or declare that your method throws the exception. It is not a case with unchecked exception.
Thanks and Regards,
cmbhatt
[ April 03, 2007: Message edited by: Chandra Bhatt ]
Nisha Pinjarkar
Greenhorn

Joined: Apr 01, 2007
Posts: 20
throw new Exception();

I already tried this way, this line gives compilation error. And if I pass empty string as arg to reserve method, it does not throw any exception. It takes it as a null string and the prog runs successfully with the result as

Reverse String =
main() ended
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
If the method doesn't contain a throws clause listing Exception, then you can't throw an instance of Exception from the class because it is checked.

You will need to throw an unchecked exception.

Here is an example.

Nisha Pinjarkar
Greenhorn

Joined: Apr 01, 2007
Posts: 20
Thanks a lot Keith !I got it !
Now I better understand the diff betn checked and unchecked exceptions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about exception
 
Similar Threads
Regarding stream unique identifiers
to exit from a function
EL accessing a bean with non-existant member
exception handling