aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception" Watch "Exception" New topic
Author

Exception

Kaleem Haqqi
Greenhorn

Joined: Aug 01, 2001
Posts: 22
Which statement is true?
a. the Error class is a RuntimeException
b. No exceptions are subclass of Error
c. Any statement that may throw an Error must be enclosed in a try block
d. Any statement that may throw an Exception must be enclosed in a try block
e. Any statement that may throw a RuntimeException must be enclosed in a try block
------------------
-Kaleem Haqqi
Java is mind work. Having the right frame of mind is essential.


<B>-Kaleem Haqqi</B><BR>Java is mind work. Having the right frame of mind is essential.
Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
I am trying to answer with explanation. Any one can correct me if
wrong or add as required :

a. the Error class is a RuntimeException
>> No the Error class is not a subclass of RuntimeException.
The super class of all exception classes i.e. java.lang.Throwable
has two immediate subclasses Exception and Error, so this
option is not correct.
b. No exceptions are subclass of Error
>> Yes, this option is correct.
c. Any statement that may throw an Error must be enclosed in a try block
>> It is not required.So this option is also not correct.
d. Any statement that may throw an Exception must be enclosed in a try block
>>No, we can declare it in the throws clause or put a try-catch block.
so this is not correct.

e. Any statement that may throw a RuntimeException must be enclosed in a try block

>> No, it is not required to put a try-catch block for RuntimeException as it is a unchecked exception. We need to put
a try-catch block or declare in the throws clause only if the exception is checked exception.So this one also is not correct
The correct answer is b.
james hoskins
Ranch Hand

Joined: Jun 28, 2001
Posts: 31
hi,
Error does have sub-classes like LinkageError and ThreadDeath
kind regards,
james
Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327

Error does have sub-classes like LinkageError and ThreadDeath

Hi James,
I was bit confused about this statement.More because of the word "exception" , but i guess that subclasses of Error
are also considered as exception.
So i guess this option is not correct.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
One more thing about option d: "Any statement that may throw an Exception must be enclosed in a try block"
That's a tricky one since a RuntimeException is_a Exception, so we have to be careful when reading that question. Angela gave a good answer. We must not enclose such statements in a try/catch block since we can declare the Exception is the throws clause of the method. But if the statement may throw a RuntimeException then we do not have to enclose it in a try/catch block NOR to declare the Exception in the throws clause.
Val
------------------
Valentin Crettaz
Sun Certified Programmer for the Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
Hi Val ,
First of "Congratulations" for passing the exam.I just saw your result on the results post.
All the best for the future.
I have two more questions related to exception. Hope you can
clear my doubts.
Consider the below code :

This gives a compile time error. Now what i know is that
when a method is throwing an exception it must catch
that exception or declare it in the throws clause.
The calling code ( i.e. which calls this method ) must also
either catch the exception or declare the exception in throws clause.
As in the above code, the methodA() declares that is throws Exception
and not IllegalAccessException, we need to catch Exception only
In summary, does it mean that we should catch (or declare throws ) in the calling code
those exceptions that are declared in the throws clause of the method ?
Can someone explain to me....These exception always seem to catch me...What is the best
way to handle such codes ?

2. If a super/Base class default constructor is throwing a checked exception, then
all the derived class constructors must handle that exception.
Am i right ?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Hi Angela,
Thanks for the congrats
OK first, methodA is declared to throw an Exception but throws in fact an IllegalAccessException. This means that in the calling code you have to catch an Exception (because this is the one declared in the throws clause) and not IllegalAccessException (the one thrown). In clear you have to catch (or pass on the throw of the Exception further) the exact type of the thrown Exception OR a supertype, but here you do exactly the opposite, i.e. you declare to throw an Exception and try to catch it using one of its subclass (IllegalAccessException), this can't work !
Concerning the constructors:
if a superclass of some class declares one of some of their constructors to throw a checked exception then you have to handle them in the constructor of the subclass !
In clear you're right

------------------
Valentin Crettaz
Sun Certified Programmer for the Java 2 Platform
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hey Val congratulations. It is very cool to be able to end the post with your title's information.
Just in case anyone could be confused. The derived constructors are forced to "handle" the checked exceptions declared in the base ones only declaring them. They can't catch them because it isn't possible to place any kind of super() call (in a constructor) within a try clause.


SCJP2. Please Indent your code using UBB Code
Raj Shah
Greenhorn

Joined: Sep 24, 2001
Posts: 14
Hi Jose,
Could u elaborate a bit more on the ways to handle the checked exceptions in the derived constructors.
What else can we do apart from just declaring that the derived constructor also throws the same exceptions and then enclosing the instance creation of subclass within a try-catch block in the main method of subclass.
As far as the code given by Angela, There will be one more simple error of "can't make static reference to method void methodA()"
Thanks
Raj
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
To Raj:
It is not allowed to catch (nor convenient) the exceptions launched by the parent constructor because this marks an error during the creation or initialization of the instance of the superclass. If it were permitted you would end up using an object whose superclases have not beeen well constructed.

this doen't compile because super must be the first thing in the constructor.
So you must declare the exception and it is no good idea to use an object not being properly constructed.
Otherwise if the exception was marking another condition, as for example a file was not found, and it is not related to the state of the object itself, yes you could catch the exception at the point of the creation and handle it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception