3

I am trying to keep the (beta version of my) application running as much as possible,So I placed another try-catch inside Program.cs as well in cases where some critical errors occur and shut the application down unexpectedly.And in the catch i rewrote the Application.Run() method so that the application can resume itself after being terminated for what ever reason.
Is it right to have such a plan for this specific scenario?
If it is not right,Then what else is recommended in order to keep the program running?

This is the sample code demonstrating what i mean:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Hossein;
using Pishro.Classes;

namespace Pishro
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new frmMain());
            }
            catch(Exception exc)
            {
                API.SaveAndShowLog(exc);
                Application.Run(new frmMain());
            }
        }
    }
  }
Hossein
  • 24,202
  • 35
  • 119
  • 224
  • 4
    If you need to keep it running - I pose you this question.. what if the exception occurrs again? – BugFinder Sep 25 '12 at 09:18
  • If the application exits because of an error, IMO you should log the exception and display a `MessageBox` to the user describing the error. – matthewr Sep 25 '12 at 09:20
  • 1
    Before you do this, read this. http://stackoverflow.com/questions/10061287/should-use-both-appdomain-unhandledexception-and-application-dispatcherunhandled – tomfanning Sep 25 '12 at 09:23
  • @BugFinder:Thats why i asked the question in first place. – Hossein Sep 25 '12 at 09:28
  • @MatthewRz:It is already done using that SaveAndShowLog() method.and meanwhile all occurring error logs get displayed in the management section, i just need a way to test as much as i can while program continues to work. – Hossein Sep 25 '12 at 09:30
  • You could do something as gouche as "exceptions = true; while (exceptions) { try { exceptions = fasle; Application.Run(new fromMain());} catch { exceptions = true; } } but.. there are better ways shown below. – BugFinder Sep 25 '12 at 09:35

3 Answers3

6

Globally handling exceptions is a good idea for logging and alerting.

An automatic restart policy like yours can be useful, yes. There is a risk however: If the crash has corrupted global data-structures restarting the app can have unpredictable results like silent data corruption. For example, files might still be open and locked. Locks might not have been released. Static variables might be in an undefined state. Rogue threads might still be running, unaware that the application UI was destroyed.

I recommend that you restart the app by starting a new process of your application. Let the old process die.

usr
  • 168,620
  • 35
  • 240
  • 369
  • Thankyou,So you are suggesting using `Application.Restart()` instead? – Hossein Sep 25 '12 at 09:39
  • 1
    @Hossein yes `Application.Restart` is a great idea. If that doesn't work for any reason you can also use `Process.Start`. – usr Sep 25 '12 at 09:55
1

I think your question involves a deeper question.. Should I catch all the exceptions? To keep going you could catch them all.. but all those possible exceptions that you don't expect or not know are probably bugs.

Maybe you should try to implement better error handling approach within your app. So all the exceptions are known or expected.

margabit
  • 2,924
  • 18
  • 24
  • I do have error handling inside my application,I really tried it everywhere i could, But there are situations which i cant simulate myself at the moment , since the application is a big one and has many parts which interacts with users,Thats why im trying to log errors and at the same time get the application running for more testing – Hossein Sep 25 '12 at 09:34
  • What I wanted to say is that it's ok if you use a general error-handling to log & detect possible issues with your app. But once you are aware of the exceptions, correct them because they are probably bugs. – margabit Sep 25 '12 at 09:42
1

Instead of wrapping a try catch around your application run method, consider handling the exceptions with events.

static void Main()
{
  AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form1());
}

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
  MessageBox.Show("Exception handled");
}

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{

}

Mainly the thread exception is the one you want but ideally - you would want to set up some form of logging/flagging the error to the user and still dispose of the program because it may cause the program to continue in an unfit state. Please put a button on your form and in the click event throw new Exception(""); and then the message box should display.

LukeHennerley
  • 6,344
  • 1
  • 32
  • 50