• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Abhilash Quiz

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 72.
Read the following piece of code carefully.
import java.io.IOException;

public class Question72
{
public Question72() throws IOException
{
throw new IOException();
}
}
Assume that the defination of Question72E begins with the line
public class Question72E extends Question72
It is required that none of the constructors of Question72E should throw any checked exception.

1. It can be achieved by placing the call to the superclass with a super keyword , which is placed in a try block with a catch block to handle the IOException thrown by the super class.
2. It can be achived by avoiding explicit calls to the base class constructor.
3. It cannot be done in the Java Laungage with the above definition of the base class.
--------------------------------------------------------------------------------
The Given answer is 3.
First of all, I haven't understood the question very well.
Ranchers, pls help me in first understanding the Question & then let's discuss the results.
tvs sundaram
 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi TVS,
A nice code snippet to discuss abt. First we have to remember that the constructor Question72() is throwing a checked Exception. So when u r going to throw a checked exception u have to catch it somewhere or ur program will give a compile time error. So when u r declaring a subclass Question72E which has no constructor, it will implicitely call the super class constructor which throws such an exception that is not caught in the calling constructor(Subclass's ctrc). So there will be a compile time error complaining uncaught exception. But it will work well if u modify ur subclass in this way.
class Question72E extends Question72{
Question72E() throws IOException{}
}

Carry on this thread...
------------------
azaman
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But can we define a class throwing an Exception?
I don't think so..
tvs sundaram
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. doesn't work because the call to super() must be the first thing in the constructor. Option 1 requires a "try" to be the first thing.

2. even if you don't explicitly call the default constructor, it will be called anyway. So option 2 doesn't work either.
Which leaves 3 as the correct answer.
[This message has been edited by Thomas Paul (edited August 20, 2001).]
 
Ashik Uzzaman
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi TVS,
No classes can't throw exception. But my slightly modified code shows u can declare to throw any exception in method header and need not catch them until u r not using these methods implicitely/explicitely. See the following code.

If u remove the Demo class (red colored) in the code it will just compile fine. But if u want to run the program u need a main() where u have to catch the exceptions thrown by the constructors. It's true for any other methods as main() when using this constructors. Hope it clears....
Anyway, i just checked that it's my 300th post within one and half months. How r u preapared tvs?
------------------
azaman
[This message has been edited by Ashik uzzaman (edited August 21, 2001).]
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, Thanks for your time & effort.
tvs sundaram
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic