• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception handling for overridding constractor

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody give the definition for subclass constractors for following class(give two if there are). Note, there is only one mothod/constractor in following class. Thanks.
public class PortConnector
{
public PortConnector(int port) throws IOException
{
...lot of valid code.
}
...other valid code.
}
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, I think you mean "constructor", not "constractor."
Now, given that, there are two key points to what you have here. First of all, notice that the only constructor defined in your class takes a single int as a parameter. As you've defined a constructor, a default constructor will not be created for you. (This is done if a class is created without a constructor.) This is important.
Any class that extends this class will invoke the constructor of this class when it is initialized. This is common initialization procedure. Because of this, the child class will have to invoke the parent's constructor. By default, if you don't invoke the parent's constructor with "super," the default constructor will be invoked. In this case, however, the parent class has no default constructor. Therefore, any descendent classes must invoke the parent constructor explicitly, like this:

If the invocation of super is not included, a compiler error will occur.
In addition, the constructor of the parent class can throw a checked exception, IOException. As the child class' constructor will invoke this method, it is responsible for dealing with the exception - either handling it or throwing it. However, as the call to super must be the first statement, you can't put that within a try block. Therefore, your only option is to throw that exception.

You can read more about constructors in the JLS, §8.8 Constructor Declarations.
I hope that helps,
Corey
[ August 02, 2002: Message edited by: Corey McGlone ]
 
Ken Chen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code:
--------------------------------------------------------------------------------
public class Test{ public Test() throws IOException { super(0); // You must pass an int as a parameter ... }} // OR public class Test{ public Test() { try { super(0); // You must pass an int as a parameter } catch (IOException e) { ... } ... }}
Corey, I don't think the second option is valid, because super(a) can only show up at first line. Actually, what I'm trying to prove is that your first option is the only one avaliable definition, except you can throw other subclasses of IOException, and RuntimeException. But IOException must be thrown. Can comments on this?
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey,
In your reply and code example, I think you meant to say:

Otherwise the "super(0)" wouldn't make sense (actually not compile since the Object class doesn't have a constructor with an integer argument).
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you're both right. I answered a little too hastily and didn't think this through.
The invocation of super must be the first line in a constructor (because the superclass must be initialized prior to the subclass).
Also, I forgot to append the extends statement. I'll update the above code to be something that works.
Thanks for the catches.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ken Chen:
...Actually, what I'm trying to prove is that your first option is the only one avaliable definition, except you can throw other subclasses of IOException, and RuntimeException. But IOException must be thrown. Can comments on this?


Actually, the only restriction on the constructor of any subclass of PortConnector is that they be able to throw IOException. They are free to throw whatever other exceptions you want. Take the following code example:

Notice that the constructor in Child is free to throw any exceptions, including non-descendents of the exception thrown in the Parent constructor, just as long as all checked exceptions from the Parent class' constructor are thrown. Hopefully, that gives you a better idea of how exceptions in constructors work.
Corey
[ August 02, 2002: Message edited by: Corey McGlone ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic