ChintanRajyaguru.com
SOADevelopment.com - Coming soon!
Originally posted by Chintan Rajyaguru:
FALSE
...
Here, ArrayIndexOutOfBoundException is not a checked exception but you still want to catch it. When compiler looks at the code it cannot determine if the code in try could throw an exception (because it knows only about checked exceptions).
- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
ChintanRajyaguru.com
SOADevelopment.com - Coming soon!
"If there’s no code in try block that may throw a checked exception that is specified in the catch block, then the compiler will produce an error. "
Great thanks,<br />Luco Zhao
ChintanRajyaguru.com
SOADevelopment.com - Coming soon!
SCJP2. Please Indent your code using UBB Code
Originally posted by andy lau:
how can i determine which checked exception will cause compiler fail and which not?
from A Programmer's Guide to Java Certification
Except for java.lang.RuntimeException, java.lang.Error and their subclasses, all exceptions are called checked exceptions. The compiler ensures that if a method can throw a checked exception, directly or indirectly, then the method must be explicitly deal with it.
- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Originally posted by andy lau:
maybe i havent expressed clearly. what i mean is that:
how can i determine which checked exception in the catch declaration will cause compiler fail and which not?
thank you all the same!
ChintanRajyaguru.com
SOADevelopment.com - Coming soon!
Originally posted by Chintan Rajyaguru:
FALSE
you may not have any code in the try block and still have catch block. Compiler wojn't complain to this. You know why?
What if you want to catch run time exception?? Like this
try
{
// some array operation
}
catch (ArrayIndexOutOfBoundException ae)
{
}
Here, ArrayIndexOutOfBoundException is not a checked exception but you still want to catch it. When compiler looks at the code it cannot determine if the code in try could throw an exception (because it knows only about checked exceptions).
Rob
SCJP 1.4
Originally posted by luco zhao:
But are there tips to determine exception type, checked or runtime? That is to say, how can I know ArrayIndexOutOfBoundsException is runtime exception?
Originally posted by Rob Ross:
Because ArrayIndexOutOfBounds is a RuntimeException - it's not a checked exception. You can do this with RuntimeExceptions only. If you try to do it with a checked exception(ie, any Exception that is not a RuntimeException), you will get a compiler error.
Try it. Replace ArrayIndexOutOfBoundsException with IOException and see if it still compiles.