Note that there are some explanatory texts on larger screens.

plurals
  1. POC# How to handle multiple exceptions which does all the same?
    primarykey
    data
    text
    <p>In my code I have a method with multiple catch statements, which perform all the same statement. I'm not sure this is the correct way to implement this. How would you do this? </p> <pre><code>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); } } </code></pre> <p>This is how HandleException looks like:</p> <pre><code>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())); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload