By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,273 Members | 1,032 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,273 IT Pros & Developers. It's quick & easy.

Catching exceptions

P: n/a
I understand a compilation error occurs when a method that throws no
exceptions
is the only code in a try block. What I don't understnad is why I can
specify
the catching of an Exception for a method that throws no exceptions,
but I
cannot catch an IOException for a method that throws no exceptions?

// This try/catch code complies fine, even though foo2 throws no
exceptions:
try {
c.foo2();
} catch(Exception e) {
System.out.println(e.getMessage());
}

// But this try/catch code fails with:
// exception java.io.IOException is never thrown in body of
corresponding try statement
try {
c.foo2();
} catch(java.io.IOException e) {
System.out.println(e.getMessage());
}

// Here is my method that throws no exceptions
public void foo2() {}

Can someone explain why an Exception can be specified, but not an
IOException?

TIA
- Rolf.
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Rolf wrote:
Can someone explain why an Exception can be specified, but not an
IOException?


Exception is a parent class to RuntimeException, which can be thrown by
the JRE itself. Runtime Exceptions are unchecked during compilation
(ie: you don't have to try to catch all of them), but you can catch and
handle them if you so wish. NullPointerException is one example of a
RuntimeException that is derived from Exception.

IOException OTOH is a pretty specific exception that is checked by the
compiler at compile time. It's possible for _any_ piece of code to
throw a NullPointerException (for example), but not for any piece of
code to throw an IOException.

HTH!

Brad BARCLAY

--
=-=-=-=-=-=-=-=-=
From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
The jSyncManager Project: http://www.jsyncmanager.org


Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.