• 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 thrown from constructor

 
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class PortConnector
{
public PortConnector(int port) throws IOException
{
...lot of valid code.
}
...other valid code.
}

You want to write another class CleanConnector that extends from PortConnector. Which of the following statements should hold true for CleanConector class?
1).It is not possible to define CleanConnector that does not throw IOException (or its superclass).
2). PortConnector class itself is not valid as you cannot throw exception from a constructor.
3).CleanConnector's constructor cannot throw any exception other than IOException.
4).CleanConnector's constructor cannot throw any exception other than superclass of IOException.
5).CleanConnector's constructor cannot throw any exception other than subclass of IOException.
############################################
The Correct Option Is (1).
I Want To Say That It Is Possible That subclass constructor don't throw any xception by itself.And in ...lot of code here of superclass that particular exception is handled.So How Option 1). is correct


Thanx@
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi agrah,

Even i believe that option 1 is correct....because in ur subclass...

it has to make a call to ur super class constructor...here you have

overloaded the constructor...

So in your subclass constructor you have to make an explicit call to this

overloaded constructor...Now This constructor throws exception as per its

declaration signature......

So necessarily even the subclass constructor has to account for that.....

Now to the question of

Which excpetion class- superclass or subclass of the exception should be

defined alongwith the subclass constructor..

You can define the same exception class as defined in superclass constructor

or a superclass of the exception ....


I hope you got it

Regards
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even you can have a different excpetion class in the constructor...like below

public CleanConnector(int port) throws StringIndexOutOfBoundsException, IOException {
super(10);

}

Regards
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and you cant catch the ioexception as super has to be first
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic