File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question on assertion 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 "question on assertion" Watch "question on assertion" New topic
Author

question on assertion

Preetha Vasudevan
Ranch Hand

Joined: Sep 23, 2005
Posts: 33
I found this question in one of the mock exams on assertions is says that the methodA int the class Asserttest throws Assertion error which of the following method can be legally placed in the subclass of the Asserttest

code:
public class Asserttest{
public void methodA(int i) throws AssertionError
{
assert i<1024:"Invalid value";
}
}

The options are:
1. public void methodA(int i)
2. public void methodA(int i) throws Exception
3. public void methodA(int i) throws Throwable
4. public void methodA(int i) throws Error
5. only A is the valid option

i answered as 1,5 but the answer is 1,4 how is option 4 right please explain...
Krishna Latha Grandhi
Ranch Hand

Joined: Nov 08, 2005
Posts: 110
Hi preetha vasudevan,

When we take overriding rules in to consideration, the rules are applied to checked exceptions only only only. ok.
Error is unchecked exception. That's why it not considerable.


If you override a method from a super class the checked exceptions that the subclass method declares cannot be more general than those of the superclass method. ( It is Ok to throw more specific exceptions, or not to throw any exceptions in the subclass method). In particular, if the super class method throws no checked exception at all, neither can the subclass.
Cheenu Subramanian
Ranch Hand

Joined: Aug 15, 2005
Posts: 40
Hi Preetha,
The Overriding Method can throw Exceptions that is a superclass of the exceptions thrown in the overridden method.

Since
class AssertionError extends Error ()

Option 4 is right I guess..
Cheenu Subramanian
Ranch Hand

Joined: Aug 15, 2005
Posts: 40
Extremely Sorry ..
Posted an incorrect and wrong reply
Cheenu Subramanian
Ranch Hand

Joined: Aug 15, 2005
Posts: 40
Extremely Sorry ..
Posted an incorrect and wrong reply
Preetha Vasudevan
Ranch Hand

Joined: Sep 23, 2005
Posts: 33
Thank you krishna
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question on assertion