views:

87

answers:

1

I have a class called "Website", and inside of that class I have the following property:

public HtmlForm RegisterForm
{
    get
    {
        if (Forms.RegForm != null) / Forms is a custom object called HTMLForms that is a custom list collection
        {
            return Forms.RegForm;
        }
        else
        {
            // FindPageWithGoogle Google = new FindPageWithGoogle();
            // use Google.FindRegistrationForm(this.currentUrl) method
            //throw new Exception(); // if registration form object can't be found
        }
        return Forms.RegForm;
    }
}

Would this be an efficient way of handling the error? In the case of it not being found, how would I halt the entire flow of the program if this exception is thrown? I know how to use a simple try catch, but I don't think that's enough. I believe I need to learn how to make my own custom exception handling system to handle these custom events accordingly.

Thanks for any help. Also, if you have a specific book on exception handling, since the C# books i've read thus far didn't go into the topic much, it would be greatly appreciated.

Thanks,

Cody

A: 

To terminate the code on an exception, just don't catch it.

You should create a meaningful exception class (e.g. InitializationError) and then throw that when the error occurs. Go out to the calling code that can display the message to the user and catch the exception there. You might also terminate the program at that point.

As far as books on exception handling, I think you'll find the MSDN chapter on exceptions helpful.

Jeremy Stein