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
posted
0
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
posted
0
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
posted
0
Also, see this link. If something is still unclear, just bring it up.
suresh kamsa
Ranch Hand
Joined: Jul 30, 2001
Posts: 149
posted
0
So Error,runtime Exceptions we should not throw using throws statement? Is that correct?
Anthony Villanueva
Ranch Hand
Joined: Mar 22, 2002
Posts: 1055
posted
0
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
posted
0
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
posted
0
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
posted
0
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>
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...