Catch base constructor errors with c#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How to catch errors thrown by the base class constructor in c# app?

Any help greatly appreciated.
 
SEB said:
How to catch errors thrown by the base class constructor in c# app?

I don't believe you can catch base class constructor exceptions in the
derived class constructor. It would rarely be a good idea to continue
anyway. What you *can* do is write a static method which calls the
derived class constructor and handles the exception appropriately, but
otherwise returns the reference to the newly constructed object.
 
thank you for your replay.
Can your idea be done using delegate? if so is it possible to prevent using
static methodes?

public class Visite : Resources
{
......
public delegate void hPassiveChecks(string sResult);
public Visite(hPassiveChecks handler)
{
this.GetSettings(handler);
}
public void GetSettings(hPassiveChecks handler)
{
....
handler(Result);
}
.....
}

class Signin : Visite
{
Signin():base (new Visite.hPassiveChecks(GetChecks))
{}
.....
private static void GetChecks(string sResult)
{
if (sResult != "SUCCESS")
this.halt(sResult)
......
}
....
}
 
SEB said:
thank you for your replay.
Can your idea be done using delegate? if so is it possible to prevent using
static methodes?

I don't think so - but I don't see what the benefit of using delegates
would be in the first place. I mean, you *could* design all your
classes to just not throw exceptions during construction, but that
would be a pretty severe limitation IMO.
 
Thanks for your patient.
My issue is the base class need to performe several checks like settings,
IPs, autorization, ... If one of those is not valide i need to halt the
application.
And because I have too many pages (over 100) and client change their mind ,
i want all the processing be done in the base class rather then apsx classes.
so any changes will be easy to manage. So using the delagate will register a
function of the derived class in the base class if any thing goes wrong the
base class will call that function which will have access to all page's
controls.

Is that right Jon? i dont want to make changes to every page. Exemple of
that the client came and says i want the visitor IP to be included in my
report.
And because the client is King, it made me crazy because i had to change
several pages

Thanks again for your reply.
 
SEB said:
Thanks for your patient.
My issue is the base class need to performe several checks like settings,
IPs, autorization, ... If one of those is not valide i need to halt the
application.
And because I have too many pages (over 100) and client change their mind ,
i want all the processing be done in the base class rather then apsx classes.
so any changes will be easy to manage. So using the delagate will register a
function of the derived class in the base class if any thing goes wrong the
base class will call that function which will have access to all page's
controls.

Is that right Jon? i dont want to make changes to every page. Exemple of
that the client came and says i want the visitor IP to be included in my
report.
And because the client is King, it made me crazy because i had to change
several pages

I'm afraid I still don't see what benefit adding a delegate will be. It
just seems a very confusing way of doing error handling - particularly
if you end up with a partially constructed object. One alternative
(although not much better) is to make your base class have a virtual
method which could be overridden in each of your pages, so you wouldn't
have to register delegates all over the place.
 
Basically you are trying to signal an invalid state of the object. You
have tried
throwing an exception in the base constructor. Jon has suggested
handling
the error in a static method. You may want to consider a third approach
providing an IsValid() method that returns false if the object state is
invalid.

Regards,
Jeff
 
Back
Top