The Base
constructor is always executed before any code in the Derived
constructor, so no. (If you do not explicitly define a constructor in Derived
, the C # compiler creates a public Derived() : base() { }
constructor public Derived() : base() { }
for you public Derived() : base() { }
.) This is to ensure that you do not accidentally use an object that has not yet been fully created.
What you can do is initialize part of the object in a separate method:
class Base { public virtual void Initialize() { throw new SomeKindOfException(); } } class Derived : Base { public override void Initialize() { try { base.Initialize(); } catch (SomeKindOfException) { ... } } }
var obj = new Derived(); obj.Initialize();
source share