• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

constructor doubt

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can a constructor any kind of exception in its throws clause??
& why?
regards
Lusha tak
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lusha tak
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Swati.
Hey Chin, try throwing some light on th eerror raised by the program code written by Swati
regards
lusha
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
swati bannore
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
lusha tak
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Namrata Shetty
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic