Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1235 :

Posted: Wed Aug 12, 2015 5:28 am
by sir_Anduin@yahoo.de
can you say that all subclasses of java.lang.Exceptions should be thrown by the application except its subclass runtimeException (and its subclasses) ?

Re: About Question enthuware.ocajp.i.v7.2.1235 :

Posted: Wed Aug 12, 2015 6:03 am
by admin
No, an application may decide not to throw any exception at all!

Re: About Question enthuware.ocajp.i.v7.2.1235 :

Posted: Wed Aug 12, 2015 7:33 am
by sir_Anduin@yahoo.de
of course.
My question was more regarding destinguishing the Exeption Types by their main usage.

Re: About Question enthuware.ocajp.i.v7.2.1235 :

Posted: Wed Aug 12, 2015 8:16 am
by admin
Yes, in general, checked exceptions are explicitly thrown by the code using the throw keyword.

Re: About Question enthuware.ocajp.i.v7.2.1235 :

Posted: Sun Aug 16, 2015 3:57 pm
by lionheartdamacy
Apologies if I'm missing the obvious here. Since run-time exceptions are all subclasses of Exception, why is it appropriate to throw an Exception? I read through this book and it explicitly states that throwing and catching an exception of type Exception shouldn't be done because it's too broad and you won't be able to effectively handle it.

Re: About Question enthuware.ocajp.i.v7.2.1235 :

Posted: Sun Aug 16, 2015 9:09 pm
by admin
I can't really comment on what the book says. You will need to ask the author about it. But based on what you've quoted here, it is true that you should throw as specific an exception as you can so that the receiver knows what exactly is the problem. For example, if you have an issue with i/o, you should throw IOException instead of Exception. This is a general programming practice.

But I am not sure I understand your question, "Since run-time exceptions are all subclasses of Exception, why is it appropriate to throw an Exception?". The question is not making any sense to me. Runtime exceptions (i.e. RuntimeException and its subclasses) are special exceptions that are are not required to be handled or declared in the throws clause. As explained in the explanation, such exceptions are generally caused due fault in program logic and are thrown automatically by the JVM. For example, you should throws a NullPointerException explicitly. The JVM throws it automatically when the code tries to access a null reference.
Checked exceptions (i.e. Exception and its subclasses except RuntimeException and its subclasses) are required to be handled or declared in the throws clause. These are the exceptions that are a good candidate for being thrown explicitly i.e. using the throw keyword.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1235 :

Posted: Mon Aug 17, 2015 1:53 pm
by lionheartdamacy
Sorry about that. Since run time exceptions subclass Exception, that would mean an Exception level catch would also catch any run-time exception, wouldn't it? If it's inappropriate to catch a run-time exception, then I'm not seeing why it's appropriate to catch a run-time exception in a broader Exception catch.

If the question is saying it's bad style to *specifically* catch a run-time error by name, then I guess it makes slightly more sense. But it does seem to me that, at least from a non-exam POV, catching an Exception is just as inappropriate or even more so--at least in some cases of run time exceptions, you can appropriately handle it (walking off an array, etc).

Re: About Question enthuware.ocajp.i.v7.2.1235 :

Posted: Mon Aug 17, 2015 9:04 pm
by admin
But question is not asking about catching. It is asking about throwing.