Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception?

 
Rishi Wright
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi 'All
I have a small doubt
class FrequencyException extends Exception{

}
in the above declaration does FrequencyException is a checked exception or is it a runtime exception??
Thank you
rishi
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is checked. If you want to make an unchecked exception, try extending RuntimeException.
 
Rishi Wright
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but then why the following code is not giving any error
class FrequencyException extends Exception{
public class Note{
public static void main(String argv[]){
System.out.print(n.tune()); }
public int tune(){
try{
return play(444);
}catch(Exception e){
System.out.println(e.getMessage());
}catch(FrequencyException fe){
System.out.println(fe.getMessage());
} }
public int play(int iFrequency)throws FrequencyException{
return 1; }}
}

I guess when a method throws a checked exception there MUST be a throw statement in the method.here 'play' is not having any ..
is there any thing wrong in this theory...
thank you
rishi
 
Shabbir Rahman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Throws clause in a method mean - it can throw FrequencyException, not like that it must have throw FrequencyException.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shabir: That is true. However, if there is any mention of any checked exception in a method's signature, any call to that method must either catch or declare even though original method does NOT actually throw the declared exception.
Rishi: Your code is actually catching mentioned exception. Therefore, it should compile o.k. However try following code. It will give you error:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic