473,394 Members | 1,289 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Catch base constructor errors with c#

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

Any help greatly appreciated.

Jul 21 '05 #1
6 2404
SEB <SE*@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
SEB
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)
......
}
....
}


"Jon Skeet [C# MVP]" wrote:
SEB <SE*@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3
SEB <SE*@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
SEB
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.

"Jon Skeet [C# MVP]" wrote:
SEB <SE*@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #5
SEB <SE*@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6
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

*** Sent via Developersdex http://www.developersdex.com ***
Jul 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
24
by: Steven T. Hatton | last post by:
If I understand correctly, I have no assurance that I can determine the type of a simple class instance thrown as an exception unless I explicitly catch it by name. (non-derived classes having no...
3
by: valued customer | last post by:
Is there a more concise way to do something like the the desired code below? The gripe is with the try-catch syntax. It takes *way* too many lines of code to evaluate a conditional expression...
12
by: Andrew Schepler | last post by:
When compiled with Visual C++ .NET 2003 (only), the program below aborts as though no matching catch clause is present. If the copy constructor of A is made public, it successfully catches the...
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
6
by: SEB | last post by:
How to catch errors thrown by the base class constructor in c# app? Any help greatly appreciated.
5
by: PasalicZaharije | last post by:
Hallo, few days ago I see ctor like this: Ctor() try : v1(0) { // some code } catch(...) { // some code }
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
10
by: Rahul | last post by:
Hi Everyone, I have the following exception class, class E1 { }; class E2 {
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.