Exception is swallowed by finally
Posted
by
fiction
on Stack Overflow
See other posts from Stack Overflow
or by fiction
Published on 2011-01-17T07:56:04Z
Indexed on
2011/01/17
8:53 UTC
Read the original article
Hit count: 228
static int retIntExc() throws Exception{
int result = 1;
try {
result = 2;
throw new IOException("Exception rised.");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
result = 3;
} finally {
return result;
}
}
A friend of mine is a .NET developer and currently migrating to Java and he ask me the following question about this source. In theory this must throw IOException("Exception rised.")
and the whole method retIntExc()
must throws Exception
. But nothing happens, the method returns 2.
I've not tested his example, but I think that this isn't the expected behavior.
EDIT: Thanks for all answers. Some of you have ignored the fact that method is called retIntExc
, which means that this is only some test/experimental example, showing problem in throwing/catching mechanics. I didn't need 'fix', I needed explanation why this happens.
© Stack Overflow or respective owner