Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1107 for trunk/sources


Ignore:
Timestamp:
01/09/09 21:29:19 (16 years ago)
Author:
swagner
Message:

Added a global try/catch block to show a general error message for all unhandled exceptions (#459)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab/Program.cs

    r877 r1107  
    2525using System.Xml;
    2626using System.Threading;
     27using System.Text;
    2728using HeuristicLab.PluginInfrastructure;
    2829
     
    3132    [STAThread]
    3233    static void Main(string[] args) {
    33       if (args.Length == 0) {  // normal mode
    34         Application.EnableVisualStyles();
    35         Application.SetCompatibleTextRenderingDefault(false);
    36         Application.Run(new MainForm());
    37       } else if (args.Length == 1) {  // start specific application
    38         PluginManager.Manager.Initialize();
    39 
    40         ApplicationInfo app = null;
    41         foreach (ApplicationInfo info in PluginManager.Manager.InstalledApplications) {
    42           if (info.Name == args[0])
    43             app = info;
    44         }
    45         if (app == null) {  // application not found
    46           MessageBox.Show("Cannot start application.\nApplication " + args[0] + " is not installed.\n\nStarting HeuristicLab in normal mode ...",
    47                           "HeuristicLab",
    48                           MessageBoxButtons.OK,
    49                           MessageBoxIcon.Warning);
     34      try {
     35        if (args.Length == 0) {  // normal mode
    5036          Application.EnableVisualStyles();
    5137          Application.SetCompatibleTextRenderingDefault(false);
    5238          Application.Run(new MainForm());
    53         } else {
    54           PluginManager.Manager.Run(app);
     39        } else if (args.Length == 1) {  // start specific application
     40          PluginManager.Manager.Initialize();
     41
     42          ApplicationInfo app = null;
     43          foreach (ApplicationInfo info in PluginManager.Manager.InstalledApplications) {
     44            if (info.Name == args[0])
     45              app = info;
     46          }
     47          if (app == null) {  // application not found
     48            MessageBox.Show("Cannot start application.\nApplication " + args[0] + " is not installed.\n\nStarting HeuristicLab in normal mode ...",
     49                            "HeuristicLab",
     50                            MessageBoxButtons.OK,
     51                            MessageBoxIcon.Warning);
     52            Application.EnableVisualStyles();
     53            Application.SetCompatibleTextRenderingDefault(false);
     54            Application.Run(new MainForm());
     55          } else {
     56            PluginManager.Manager.Run(app);
     57          }
    5558        }
    5659      }
     60      catch (Exception ex) {
     61        ShowErrorMessageBox(ex);
     62      }
     63    }
     64
     65    public static void ShowErrorMessageBox(Exception ex) {
     66      MessageBox.Show(BuildErrorMessage(ex),
     67                      "Error - " + ex.GetType().Name,
     68                      MessageBoxButtons.OK,
     69                      MessageBoxIcon.Error);
     70    }
     71
     72    private static string BuildErrorMessage(Exception ex) {
     73      StringBuilder sb = new StringBuilder();
     74      sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
     75
     76      while (ex.InnerException != null) {
     77        ex = ex.InnerException;
     78        sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
     79      }
     80      return sb.ToString();
    5781    }
    5882  }
Note: See TracChangeset for help on using the changeset viewer.