This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question:about exceptions

 
Wei Lien Chun
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void m(){
try{}
catch(Exception e){}
}
public void m1(){
try{}
catch(IOException e){}
}
Why does the method m1() complain but m() does not since it too does not throw any exception in try ? Why is it allowed also to throw exceptions in the method declaration without the method actually throwing any (blank method for eg)? any help will be appreciated.
Thanks!
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a) speculation: It's easy to find out for the compiler, whether a checked Exception might be thrown by a method or not.
RuntimeExceptions (OutOfMemory, ArrayIndexOutOfBounds) are much harder, nearly impossible excluded. Exception: the very seldom case of empty methods.
b) You need the possibility of declaring an exception to be thrown, to allow derived classes to throw that exception.
 
David Peterson
author
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "complain"? Compilation error? RuntimeException?
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compilation errors
 
Amit Agrawal
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Wei Lien Chun:
public void m(){
try{}
catch(Exception e){}
}
public void m1(){
try{}
catch(IOException e){}
}
Why does the method m1() complain but m() does not since it too does not throw any exception in try ? Why is it allowed also to throw exceptions in the method declaration without the method actually throwing any (blank method for eg)? any help will be appreciated.
Thanks!

Exception is parent class to both Runtime and Checked Exceptions. When you catch 'Exception class' object, compiler allows you as it can not be certain as you might would have tried catching it anticipating some runtime exceptions while in case of Checked Exception (IOException), it checks possibility of code throwing the particular exception and when it finds that there is no such possibility of generating this exception, it complains!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic