aspose file tools*
The moose likes Beginning Java and the fly likes Question:about exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question:about exceptions" Watch "Question:about exceptions" New topic
Author

Question:about exceptions

Wei Lien Chun
Greenhorn

Joined: Mar 18, 2004
Posts: 5
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

Joined: Jun 02, 2003
Posts: 1923

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.


http://home.arcor.de/hirnstrom/bewerbung
David Peterson
author
Ranch Hand

Joined: Oct 14, 2001
Posts: 154
What do you mean by "complain"? Compilation error? RuntimeException?
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

compilation errors
Amit Agrawal
Ranch Hand

Joined: Aug 23, 2001
Posts: 282
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question:about exceptions
 
Similar Threads
Exceptions
Exception Handling
Exceptions error
Try/catch flow???
Assertion Exception handling