Ketung Xiao wrote:
so why the following code compile ?
All things are lawful, but not all things are profitable.
Ketung Xiao wrote:
My reasoning is that unchecked are not actually catched exception by Java , it'll never be catched, but will be a runtime error. Otherwise, why Java make the distinction between error, runtime error and checked exceptions ? Isn't it misleading to have the code compiled but not actually performed ?
Ketung Xiao wrote:I'm more confused than was before.
The following code will compile. But it calls Boolean kh = Array.getBoolean(value, 5) which has this method signature:
[**Code Deleted**]
which make me think, caller must catch or throw !!! otherwise it should not compile
Ketung Xiao wrote:the following code will compile; but isn't IndexOutOfBoundsException a subclass of RunTimeException, therefore it's not a checked exception ?
so why the following code compile ?
Please help to explain.
catch (InputMismatchException[/i]
Marius Zilenas wrote:I think it compiles because it is not forbidden to catch runtimeexceptions.
You can catch any kind of exception with a small e. If you look in the Java® Language Specification (=JLS), it tells you that,Ketung Xiao wrote:. . . it is not forbidden to catch runtimeexceptions.
Beware: the JLS can be difficult to read. If you look up the details of IndexOutOfBoundsException, you will see it is a subclass of Throwable, so it counts as an exception class (small e).An exception is represented by an instance of the class Throwable (a direct subclass of Object) or one of its subclasses.
Throwable and all its subclasses are, collectively, the exception classes. . . .
That doesn't look correct, I am afraid. Every instance of all exception classes can be caught, but as Henry has already said, you can only catch a checked subclass of Exception if it can actually be thrown in the try block.. . . if the code catch runtimeexceptions or "uncheck" exception which include error exception, it will work just like "checked" exception.
No. Old posts can be useful and we often see people benefiting from posts over ten years oldBy the way, is there a way to delete my own quote after I submit a post ?
Ketung Xiao wrote:So, you are say: IndexOutOfBoundsException which is subclass of java.lang.RuntimeException CANNOT be caught or thrown ?
No, I said that all exception classes' objects can be caught and the IndexOutOfBoundsException is an exception class.Ketung Xiao wrote:So, you are say: IndexOutOfBoundsException . . . CANNOT be caught or thrown ? . . .
Ketung Xiao wrote:
The following code will run:
All things are lawful, but not all things are profitable.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |