Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about exception

 
Nisha Pinjarkar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain more about the question?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nisha,

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



Thanks and Regards,
cmbhatt
 
Nisha Pinjarkar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Keith !I got it !
Now I better understand the diff betn checked and unchecked exceptions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic