About Question enthuware.ocajp.i.v7.2.1235 :
Moderator: admin
-
- Posts: 62
- Joined: Fri Aug 07, 2015 2:16 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1235 :
can you say that all subclasses of java.lang.Exceptions should be thrown by the application except its subclass runtimeException (and its subclasses) ?
Online
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1235 :
No, an application may decide not to throw any exception at all!
If you like our products and services, please help us by posting your review here.
-
- Posts: 62
- Joined: Fri Aug 07, 2015 2:16 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1235 :
of course.
My question was more regarding destinguishing the Exeption Types by their main usage.
My question was more regarding destinguishing the Exeption Types by their main usage.
Online
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1235 :
Yes, in general, checked exceptions are explicitly thrown by the code using the throw keyword.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sun Aug 16, 2015 3:53 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1235 :
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.
Online
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1235 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sun Aug 16, 2015 3:53 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1235 :
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).
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).
Online
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1235 :
But question is not asking about catching. It is asking about throwing.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 223 guests