aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception question from Dan Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception question from Dan" Watch "Exception question from Dan" New topic
Author

Exception question from Dan

suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
class A {
2. void m1() {throw new ClassNotFoundException();}
3. void m2() {throw new ArithmeticException();}
4. void m3() {throw new ClassCastException();}
5. void m4() {throw new IllegalArgumentException();}
6. void m5() {throw new CloneNotSupportedException();}
7. void m6() {throw new NoSuchFieldException();}
8. }
I got compiler error at line 2 and 6 not at 7. Can anyone compile tell me whether u r getting compile error at line 7, cuz Dan's answer saying that compile error at line 7 also.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
This question tests the difference between checked and unchecked exceptions. If a method throws a checked exception, it must be specified in the method's throws clause.
suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
Can you expalin me what do you mean by checked exceptions and unchecked exceptions.
Is runtime exceptions are unchecked exceptions?
Thanks.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
RuntimeException and Error (and all subclasses of each) are unchecked.
All other throwables are checked.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Also, see this
link. If something is still unclear, just bring it up.
suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
So Error,runtime Exceptions we should not throw using throws statement? Is that correct?
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
It's usually a good idea to leave errors alone. That is, errors are not normally considered in programmatic exception handling. You don't have to specify Runtime exceptions in a method's throws clause, even if you know it may throw this error.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Let me say that in a better way: the compiler will not compel you specify Runtime exceptions in a method's throws clause, even if you know it may throw this error.
suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
Crystal Clear now.
Bottom line is I should know whic type of exceptions comes under which class.
Thanks.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by suresh kamsa:
So Error,runtime Exceptions we should not throw using throws statement? Is that correct?

Suresh,
You are correct. Application code should not throw RuntimeExceptions using an explicit "throw" statement. For example, application code should not include statements such as the following.
throw new ArithmeticException();
I violated that rule in my mock exam because I did not want to complicate the exam questions by
creating code with bugs that would result in the JVM throwing the same exception. In other words, using the explicit "throw" statement produced easier exam questions.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I'm not sure I agree. There are times when throwing NullPointerException or IndexOutOfBoundsException or IllegalArgumentException is the right response to an invalid call on one of your methods.
[ August 22, 2002: Message edited by: Ron Newman ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I agree with you Ron.
Dan,
If you have to get non null arguments in your methods, you have to perform a null check before using the arguments. If one of them happens to be null, the caller has to know about it (that it broke the contract), and usually this boils down to throwing an instance of IllegalArgumentException, a subclass of RuntimeException. There are plenty of examples in Sun's Java source code...


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
 
subject: Exception question from Dan