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
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:
There is no emoticon for what I am feeling!
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