471,310 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

Constructor Chaining

I have a class A that is inheriting from class B. Class B has a
constructor that I would like to use in Class A, so I chain to it.
However class B's constructor often misbehaves and it causes a mess
and throws a meaningless error. I would like to wrap the call to class
B's constructor in a try/catch so that I can at least throw something
of use. I can't see how I would do this with chaining however.

Everything that class B calls in its constructor has been marked
internal or private so I can't just decompile it and copy and paste
its constructor code out without copying several other functions (all
of which also call private and internal methods - you can see this
would get out of control).

In C# you chain by doing:

public MyClassName() :[this or base](arguments)
{
}

what I ned is something more like:

public MyClassName()
{

}
Sep 11 '08 #1
8 1921
On Sep 11, 2:59*pm, escher4096 <came...@ogmios.cawrote:
I have a class A that is inheriting from class B. Class B has a
constructor that I would like to use in Class A, so I chain to it.
However class B's constructor often misbehaves and it causes a mess
and throws a meaningless error. I would like to wrap the call to class
B's constructor in a try/catch so that I can at least throw something
of use. I can't see how I would do this with chaining however.

Everything that class B calls in its constructor has been marked
internal or private so I can't just decompile it and copy and paste
its constructor code out without copying several other functions (all
of which also call private and internal methods - you can see this
would get out of control).

In C# you chain by doing:

public MyClassName() :[this or base](arguments)
{

}

what I ned is something more like:

public MyClassName()
{

}
oops miscued and posted by accident :)

what I need is something more like:

public MyClassName()
{
try
{
[this or base](arguments);
}
catch(blah blah blah)
{
throw new meaningful exception....
}
}

Is it possible to do this in C#?

Thanks

-Cam
Sep 11 '08 #2
On Sep 11, 2:59*pm, escher4096 <came...@ogmios.cawrote:
I have a class A that is inheriting from class B. Class B has a
constructor that I would like to use in Class A, so I chain to it.
However class B's constructor often misbehaves and it causes a mess
and throws a meaningless error. I would like to wrap the call to class
B's constructor in a try/catch so that I can at least throw something
of use. I can't see how I would do this with chaining however.

Everything that class B calls in its constructor has been marked
internal or private so I can't just decompile it and copy and paste
its constructor code out without copying several other functions (all
of which also call private and internal methods - you can see this
would get out of control).

In C# you chain by doing:

public MyClassName() :[this or base](arguments)
{

}

what I ned is something more like:

public MyClassName()
{

}
oops miscued and posted too soon :)

what I need is something more like:

public MyClassName()
{
try
{
base(arguments);
}
catch(blah blah blah)
{
thgrow new meaningful exception
}
}

Is this possible in C#?

Thanks

-Cam

Sep 11 '08 #3
On Thu, 11 Sep 2008 13:59:03 -0700, escher4096 <ca*****@ogmios.cawrote:
I have a class A that is inheriting from class B. Class B has a
constructor that I would like to use in Class A, so I chain to it.
However class B's constructor often misbehaves and it causes a mess
and throws a meaningless error. I would like to wrap the call to class
B's constructor in a try/catch so that I can at least throw something
of use. I can't see how I would do this with chaining however.
You can't do it.

If the constructor throws an exception, then the object isn't properly
constructed and thus is unusable. I never thought about it before, but in
hindsight this may be why C# puts the call to a base constructor outside
the method body, rather than inside the way Java does it, to make it very
clear that you can't legitimately catch exceptions thrown by base
constructors.

If your class would be usable even though the base class isn't properly
constructed, then your own class probably shouldn't be inheriting that
class anyway. You may be better off compositing the class into your own,
and providing whatever fallback implementation is appropriate when you
can't create an instance of class B.

Pete
Sep 11 '08 #4
On Sep 11, 2:10*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
>
If your class would be usable even though the base class isn't properly *
constructed, then your own class probably shouldn't be inheriting that *
class anyway. *You may be better off compositing the class into your own, *
and providing whatever fallback implementation is appropriate when you *
can't create an instance of class B.
Yes, I agree with Pete here. I say make the misbehaving base class
"virtual" so that it doesn't do much, and let the derived class do the
real work.

RL

Sep 11 '08 #5
On Sep 11, 9:59*pm, escher4096 <came...@ogmios.cawrote:
I have a class A that is inheriting from class B. Class B has a
constructor that I would like to use in Class A, so I chain to it.
However class B's constructor often misbehaves and it causes a mess
and throws a meaningless error. I would like to wrap the call to class
B's constructor in a try/catch so that I can at least throw something
of use. I can't see how I would do this with chaining however.
<snip>

As others have said, you can't do this. What you *can* do is make your
constructor private and expose static methods to create instances.
They can do error handling however you want.

Jon
Sep 12 '08 #6
On Sep 12, 2:02*am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Sep 11, 9:59*pm, escher4096 <came...@ogmios.cawrote:
I have a class A that is inheriting from class B. Class B has a
constructor that I would like to use in Class A, so I chain to it.
However class B's constructor often misbehaves and it causes a mess
and throws a meaningless error. I would like to wrap the call to class
B's constructor in a try/catch so that I can at least throw something
of use. I can't see how I would do this with chaining however.

<snip>

As others have said, you can't do this. What you *can* do is make your
constructor private and expose static methods to create instances.
They can do error handling however you want.

Jon
Or wrap A's construction in try/catch and handle, because as Peter
said, you don't want the A object whose base B construction failed
anyway.
Sep 12 '08 #7
On Sep 12, 4:35*pm, "G.S." <gstoy...@gmail.comwrote:
Or wrap A's construction in try/catch and handle, because as Peter
said, you don't want the A object whose base B construction failed
anyway.
But if the purpose is to rewrap the exception in a more meaningful
one, you don't want to have to do that at *every* call site. By
wrapping it within a single static method, class A is correctly taking
responsibility for things.

Jon
Sep 12 '08 #8
On Sep 12, 11:48*am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Sep 12, 4:35*pm, "G.S." <gstoy...@gmail.comwrote:
Or wrap A's construction in try/catch and handle, because as Peter
said, you don't want the A object whose base B construction failed
anyway.

But if the purpose is to rewrap the exception in a more meaningful
one, you don't want to have to do that at *every* call site. By
wrapping it within a single static method, class A is correctly taking
responsibility for things.

Jon
yes, yes, you're right, of course but I guess I was thinking that if
its rethrown, it's gotta be handled
Sep 12 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

45 posts views Thread by Ben Blank | last post: by
19 posts views Thread by Rob | last post: by
7 posts views Thread by Geoffrey | last post: by
3 posts views Thread by needin4mation | last post: by
2 posts views Thread by DaTurk | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.