Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

subclass constructor exceptions

 
Ol Holmes
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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 ]
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic