| Author |
error in try catch
|
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
this gives me a compile error that the try block doesnt throw an ioexception.......i had to remove io from the catch and make it catch(Exception) and then this compiled
but why is this even happening? why does the compiler complain first of all
thanks
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
IOException is a checked exception. So if you catch it, the compiler will make sure that its actually thrown in the body of the associated try block.
Now Exception is also a checked exception. But it has an unchecked subclass RuntimeException. So the compiler will not check if Exception is actually thrown in the body of the try block as the try block might throw a RuntimeException which the compiler is not worried about...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
|
You mean an ioexception is not thrown in the try block....how is that? i mean when will a try block throw ioexceptions?
|
 |
Dejan Miler
Ranch Hand
Joined: Nov 14, 2009
Posts: 56
|
|
Look in Java documentation.
Method getName do not throw any exception.
IOException will be thrown if you modify you code like this
|
SCJP 1.6 in progress ....
|
 |
 |
|
|
subject: error in try catch
|
|
|