Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab/Program.cs @ 1823

Last change on this file since 1823 was 1107, checked in by swagner, 16 years ago

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

File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Windows.Forms;
25using System.Xml;
26using System.Threading;
27using System.Text;
28using HeuristicLab.PluginInfrastructure;
29
30namespace HeuristicLab {
31  static class Program {
32    [STAThread]
33    static void Main(string[] args) {
34      try {
35        if (args.Length == 0) {  // normal mode
36          Application.EnableVisualStyles();
37          Application.SetCompatibleTextRenderingDefault(false);
38          Application.Run(new MainForm());
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          }
58        }
59      }
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();
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.