C# Wie, um mehrere Ausnahmen, die nicht alle gleich?

In meinem code habe ich eine Methode mit mehreren catch-Anweisungen, die führen Sie alle die gleiche Aussage. Ich bin mir nicht sicher, ob dies der richtige Weg, um dies zu implementieren. Wie würden Sie dies tun?

public void LoadControl(ControlDestination controlDestination, string filename, object parameter)
{
    try
    {
        //Get filename with extension
        string file = GetControlFileName(filename);

        //Check file exists
        if (!File.Exists(file))
            throw new FileNotFoundException();

        //Load control from file
        Control control = LoadControl(filename);

        //Check control extends BaseForm
        if (control is BaseForm)
        {
            //Set current application on user control
            ((BaseForm)control).CurrentApplication = this;
            ((BaseForm)control).Parameter = parameter;

            //Set web user control id
            control.ID = filename;

            Panel currentPanel = null;

            switch (controlDestination)
            {
                case ControlDestination.Base:
                    //Set current panel to Base Content
                    currentPanel = pnlBaseContent;
                    //Set control in viewstate
                    this.BaseControl = filename;
                    break;
                case ControlDestination.Menu:
                    //Set current panel to Menu Content
                    currentPanel = pnlMenuContent;
                    //Set control in ViewState
                    this.MenuBaseControl = filename;
                    break;
            }

            currentPanel.Controls.Clear();
            currentPanel.Controls.Add(control);
            UpdateMenuBasePanel();
            UpdateBasePanel();

        }
        else
        {
            throw new IncorrectInheritanceException();
        }
    }
    catch (FileNotFoundException e)
    {
        HandleException(e);
    }
    catch (ArgumentNullException e)
    {
        HandleException(e);
    }
    catch (HttpException e)
    {
        HandleException(e);
    }
    catch (IncorrectInheritanceException e)
    {
        HandleException(e);
    }

}

Dies ist, wie HandleException aussieht:

private void HandleException(Exception exception)
{
    //Load error control which shows big red cross
    LoadControl(ControlDestination.Menu, "~/Controls/Error.ascx", null);

    //Store error in database
    DHS.Core.DhsLogDatabase.WriteError(exception.ToString());

    //Show error in errorbox on master
    Master.ShowAjaxError(this, new CommandEventArgs("ajaxError", exception.ToString()));
}
  • Fang nicht ArgumentNullException, vermeiden Sie es!
  • Ich denke, dass dies der Weg würde ich wählen, dies zu tun - ja es ist ein bisschen langatmig, aber Sie sind explizit, was Sie tun, und es ermöglicht es Ihnen, zusätzliche geeignete Schritte auf jeden Fall
InformationsquelleAutor Martijn | 2009-11-10
Schreibe einen Kommentar