• 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

public ConstructorTest(int j) throws Exception

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can a Constructor specify exceptions in the header?
Well, as I found out by writing the following code, the answer is yes.
But according to Khalid Mughal's book, page 101, it is stated that "Constructors cannot specify exceptions in the header".

Output from the program -
Value of parameter is: 10
Value of parameter is: Passed
Could someone please elaborate?
Thank you
Prasanna.

[This message has been edited by Prasanna Wamanacharya (edited May 24, 2001).]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// A public constructor throwing an exception.
public ConstructorTest(int j) throws Exception {
i = j;
System.out.println("Value of parameter is: " + i);
}

// Another public constructor throwing an exception.
public ConstructorTest(String s) throws Exception {
st = s;
System.out.println("Value of parameter is: " + st);
}

public static void main(String args[]) {
try {
ConstructorTest c1 = new ConstructorTest(10);
ConstructorTest c2 = new ConstructorTest("Passed");
}
catch(Exception e) {}
}
}

Output from the program -
Value of parameter is: 10
Value of parameter is: Passed

hi friend , u have come to the right point ,
obviously constructors can throw exceptin at any time,
but now there is one problem with that, u have taken a simple example,...but if there is one super class , the sub class extending the super one,
now if i make an instance of a sub class ....the sub class constructor will call supers constructor implicitly,
so if the Super's constuctor throws an Exception ...it has to
catch in subclass constructor....so there must be try block in Sub's constructor....BUT....then try become before SUPER call statement...
which is not allowed ie SUPER must be first statement in Constructor .....so
FOR THIS problem there is no solution in JAVA....
got it.........bye
dheeraj.....

 
Prasanna Wamanacharya
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dheeraj,
Yes, it does get a little complicated when a superclass - subclass relation is involved.
I browsed through the API to check for constructors throwing exceptions, and I found out quite a few in the java.io package, eg. RandomAccessFile class has two constructors, both throwing exceptions. But their super class i.e Object's constructor doesn't throw an exception.
I tried sub-classing RandomAccessFile to check if I could provide the subclass with a constructor that doesn't throw exceptions. I couldn't suceed because of the implicit super() call in the subclass constructor... RandomAccessFile does not have a default constructor! I thought of searching for a class in the API which has a default constructor, and which also throws exceptions, but gave up the thought as I have scheduled the test on 4th May, and I don't have time to do that. Maybe after the test I shall search the API.
Anyways, as a side-effect I have found out that we cannot subclass a superclass which does not have a default constructor. Correct me if I am wrong.
Thanks again
Prasanna.
reply
    Bookmark Topic Watch Topic
  • New Topic