This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Construtor and Member variables

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


Class B
{
someMethod(){
//throws an exception
}
}

Class A
{
B memX = B.someMethod();

A() {
//This constructor is not called
}

}

Class D {
someMeth() {
Class A = new A();
}}

As u see the memX object of type B is intialized by calling static method someMethod(). This method throws an exception,
My question is if it throws an exception will this result in my constructor for class A not being invoked.
How can I fix this issue. Should I catch the exception at the time of instantiating an object of Class A.
Thnx
Deepak
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hope this helps...

class A
{
B memX;
A() {
try{
memX = B.someMethod();
}
catch(Exception e){
}
System.out.println("Calling A()!");
}
}
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q: Will this result in my constructor for class A not being invoked?
A: Why not run your code and see:

...and if you think about it, if A's constructor *isn't* invoked, then the exeception *won't* be thrown -- and does that make sense?

Q: How can I fix this issue. Should I catch the exception at the time of instantiating an object of Class A.

If a constructor isn't successful it's much better if it throws an exception, compared to silently constucting a broken object. You may want the exception thrown by the static method to propagate out of A's constructor, or you may want to catch it in A's construtor and throw an exception that's more appropriate for A's abstraction:

Finally, client code will either catch the exception thrown by A's constructor or propagate it:
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic