New to C#

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

Guest

Hello, Newsgroupians:

I'm new to C#, and I'd like to ask for your input on a couple of questions
of mine.

First, I am creating a Windows application. In my main statement, I have
one line...

public static int Main(string[] astrArgs)
{
System.Windows.Forms.Application.Run(new QualWnd());
}

I the QualWnd() constructor, I have a try and catch statement. If I catch
the exception, I want the application to close, so in my exception I do the
following... this.Close(); However, an exception is raised when the
program control returns to Main(...). It indicates that it "cannot access a
disposed object," which I assume is my QualWnd.

What can I do to stop the program if my constructor of my form throws an
exception?

Second, I've purchased Charles Petzold's "Programming Windows with C#." I
think Mr. Petzold is an amazing author, especially his "Programming Windows,"
using Win32. As such, I bought his book, hoping it would be as informative.
However, "Programming Windows with C#" -- I believe -- is lacking his
traditional finesse and many technical details. It's only my opinion. Does
anyone have any other recommendations?

Thank you,


Trecius
 
[...]
What can I do to stop the program if my constructor of my form throws an
exception?

Well, that's just it. Your constructor doesn't throw an exception. It's
catching it and changing it into invalid behavior (namely, closing the
form rendering the class instance invalid from within the constructor).

Your constructor should either return normally, with the class instance in
a valid state, or it should throw an exception itself. IMHO, no other
behavior is acceptable.

You can deal with the error in a couple of ways. The old-fashioned way
would be to include a "ref" boolean that indicates success or failure.
Then the caller could check the boolean and proceed as necessary.
Alternatively, the constructor could just allow the exception to bypass it
and you could handle it where you attempt to construct the form (the form
may still need to handle the exception, if it needs to clean something up
to ensure that it's in a consistent state, but it would then rethrow the
exception after doing it's own thing). IMHO, this is the preferable
method.

So your code would look something like this:

public static int Main(string[] astrArgs)
{
try
{
System.Windows.Forms.Application.Run(new QualWnd());
}
catch
{
// do whatever error handling here
}
}

If you want to be especially tidy, you could keep a local variable that
takes the constructed form and closes it explicitly in the catch clause.
But if you're application is going to exit anyway, this doesn't seem
necessary to me, as long as the form doesn't have other stuff lying around
that it cleans up on close (a non-background thread, for example).

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top