Hi, After reading ur query I tried following code. It does allow Constructor to throw Exception but when I extend this class and write a try/catch block,I am getting some error.Just chk out where it is wrong,
It gives me error, Type Expected.. Since subclass constructor will make an call to superclass constructor, i am wrting this in try/catch block. pls, correct
[This message has been edited by swati bannore (edited July 18, 2001).]
The throws clause for a constuctor is similar to that of any other method..Constuctor can throw Exceptions like any other methods.... hope thiz helps chin
what swati has done if we can do like this then we dont have have to define any method so instead of usinf try..catch simple use throw in the sub-class constructor n see it works or not. asheet
Hi asheet, It does work if I throw it again from my subclass constructor. But What if I do not want to throw it further and catch it there. How do I do it then without try / catch? Can Jane help? Thanx
Please see the following code to deal with the exception thrown by constructor.
The point is: <code> If parent class default constructor is throwing an exception, the derived class default constructor should handle the exception thrown by the parent. </code> Hope it helps. Guoqiao
[This message has been edited by Guoqiao Sun (edited July 18, 2001).]
Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="http://www.jiris.com/" target="_blank" rel="nofollow">www.jiris.com</a>, Java resource, mock exam, forum
this itself is the point of doubt! how do we handle the exception in subclass itself.(for example consider the case when we don't have the main method defined in subclass) regards Lusha
class base{ base() throws Exception {throw new Exception();} } class derived extends base { derived() throws Exception{} } public class test2 { public static void main (String[] args){ try{ derived d =new derived(); } catch (Exception e){ System.out.println("Exception caught");} } } i'm not so sure how u can catch the exception in the subclass itself; but the above code could be a possible solution when your main() method is not in the subclass
class base{ base() throws Exception {throw new Exception();} } class derived extends base { derived() throws Exception{} } public class test2 { public static void main (String[] args){ try{ derived d =new derived(); } catch (Exception e){ System.out.println("Exception caught");} } } i'm not so sure how u can catch the exception in the subclass itself; but the above code could be a possible solution when your main() method is not in the subclass
you will have to handle the exception where you will make a call to derived class constructor, or in other words where you will make an instance of the derived class. Hope this will help to understand
The fastest and most reliable components of any system are those that are not there. Tiny ad:
Gift giving made easy with the permaculture playing cards