• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception question from Dan

 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RuntimeException and Error (and all subclasses of each) are unchecked.
All other throwables are checked.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, see this
link. If something is still unclear, just bring it up.
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Error,runtime Exceptions we should not throw using throws statement? Is that correct?
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Crystal Clear now.
Bottom line is I should know whic type of exceptions comes under which class.
Thanks.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic