• 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:

subclass constructor exceptions

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
My understanding of this topic is as follows..
If the ParentClass constructor throws an exception, the ChildClass constructor must also throw that exception (or a super class of that exception) if a call to super is made in the constructor.
Is this correct?
Many thanks
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Welcome to JavaRanch!
Yes. Note that the call to the superclass constructor can be implicit -- i.e., if you don't explicitly call a constructor, the compiler inserts a call to super() .
[ September 10, 2003: Message edited by: Ernest Friedman-Hill ]
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy,
Here's one way to think about the implications:
If your superclass constructor declares an exception, your subclass constructor must also declare that exception.
What does this actually mean? Several things...
1) You MUST write a constructor in your class! You can't just allow for the compiler to insert the default constructor, because you must have a place to declare the exception.
class Foo {
Foo() throws FooException { }
}
class Bar extends Foo {
Bar() throws FooException { } // we MUST have this
}
In this example, even though we don't *need* a constructor in Bar for any other reason (in other words, we don't need any code in our constructor) we must still put in the constructor just so that we have a place to declare the exception!
2) Constructors are an EXCEPTION to the "Handle or Declare" law
(sorry about the overloaded use of the word 'exception' )
With constructors, there is no option to "Handle" -- you can ONLY declare. Because, where would you *put* the try/catch?
class Bar extends Foo {
Bar() {
try {
super();
} catch (FooException fe) { } // will not work!
}
}
You already KNOW why this is not possible... because the first line of every constructor must be either the call to super() or this(), and here you are trying to put the 'try' ahead of the super(). Not possible.
<side-note>
This is not covered in the current exam, but there are several areas in Java where this is an issue. Most notably, if you do RMI, where your Remote class extends UnicastRemoteObject, whose constructor does indeed declare a RemoteException. So your Remote class then must explicitly declare a constructor, so that you have a place to declare the exception declared by your superclass constructor.
</side-note>
cheers
Kathy
 
Kathy Sierra
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy,
Here's one way to think about the implications:
If your superclass constructor declares an exception, your subclass constructor must also declare that exception.
What does this actually mean? Several things...
1) You MUST write a constructor in your class! You can't just allow for the compiler to insert the default constructor, because you must have a place to declare the exception.
class Foo {
Foo() throws FooException { }
}
class Bar extends Foo {
Bar() throws FooException { } // we MUST have this
}
In this example, even though we don't *need* a constructor in Bar for any other reason (in other words, we don't need any code in our constructor) we must still put in the constructor just so that we have a place to declare the exception!
2) Constructors are an EXCEPTION to the "Handle or Declare" law
(sorry about the overloaded use of the word 'exception' )
With constructors, there is no option to "Handle" -- you can ONLY declare. Because, where would you *put* the try/catch?
class Bar extends Foo {
Bar() {
try {
super();
} catch (FooException fe) { } // will not work!
}
}
You already KNOW why this is not possible... because the first line of every constructor must be either the call to super() or this(), and here you are trying to put the 'try' ahead of the super(). Not possible.
<side-note>
This is not covered in the current exam, but there are several areas in Java where this is an issue. Most notably, if you do RMI, where your Remote class extends UnicastRemoteObject, whose constructor does indeed declare a RemoteException. So your Remote class then must explicitly declare a constructor, so that you have a place to declare the exception declared by your superclass constructor.
</side-note>
cheers
Kathy
 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic