user541597
user541597

Reputation: 4345

Getting exception that caused program to crash without being in debug mode

I have an application in C# that I want to run by just running the .exe from my desktop. However, I'm pretty sure there will be some type of error that will make the program crash. Is there a way to write the problem that caused the program to crash to a text file, so that I can see what caused the issue when users are using the program? I know I can use debug mode to do this but I want to run the application as a stand alone not inside of VS.

Thanks,

I am aware of the try catch blocks and I am already using those where problems might occur. But I am speaking in general. For example if I wasn't sure where the problem would occur. There is no way to print this specific error to a file.

Upvotes: 3

Views: 2211

Answers (6)

Druegor
Druegor

Reputation: 389

See initial information here http://www.csharp-examples.net/catching-unhandled-exceptions/

static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
  MessageBox.Show(e.Exception.Message, "Unhandled Thread Exception");
}

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
  MessageBox.Show((e.ExceptionObject as Exception).Message, "Unhandled UI Exception");
}

The UnhandledException event handles uncaught exceptions thrown from the main UI thread. The ThreadException event handles uncaught exceptions thrown from non-UI threads.

I would replace the MessageBox with some actual logging (log4net or others). This would give you the ability to log out the errors to another server for distributed applications, file system for local users, event logs, options are fairly unlimited if you're willing to put in the time.

Upvotes: 0

Mike Zboray
Mike Zboray

Reputation: 40818

You can try the global try/catch method except that if there is an exception on a background thread it won't be caught. You can use AppDomain.UnhandledException if you want to be notified of any unhandled exception in the appdomain (msdn). You would signup in main before the rest of your program executes like so:

static void Main(string[] args)
{
  AppDomain.UnhandledException += WriteUnhandledExceptionToFile;

  // rest of program
}

static void WriteUnhandledExceptionToFile(object sender, UnhandledExceptionEventArgs args)
{
   // write to where ever you can get it.
   string path = Path.Combine(Environment.CurrentDirectory, "UnhandledException.txt");
   File.WriteAllText(path, args.ExceptionObject.ToString()); // will print message and full stack trace.
}

Edit

Note that by default Windows Forms and WPF catch any exceptions that are thrown on the UI thread. You will have to subscribe to the Application.ThreadException event (forms) or Application.DispatcherUnhandledException event (wpf) to be notified of exceptions on those threads. The code would be very similar to the code above for the AppDomain event.

Upvotes: 2

om471987
om471987

Reputation: 5627

you can surround your one of the starting method with try catch block

try
{
   ///Your code
}
catch(Exception exception)
{
   System.IO.File.WriteAllLines("ErrLog.txt", exception.Message);
}

As a permanent solution you can create extension method ToLog and use it whenever you want.

public static void ToLog(this Exception Exception)
{
        using (var file = File.AppendText("ErrorLog.txt"))
        {
           file.WriteLine(DateTime.Now + " : " + exception.Message);
        }
}

You can use it in catch block like this

 catch(Exception exception)
 {
    exception.ToLog();
 }

Upvotes: 0

David
David

Reputation: 218818

The first thing you want to look at is the try/catch construct in C#. This is probably your first building block to handling errors.

As for how you handle the errors, that's entirely up to you. Currently your only stated goal is to log them to a file. You can get a lot of details out of the Exception object that you catch and you can write those details to a file. Additionally, you can use logging libraries to help with that sort of thing.

Proper error handling is something of a big subject, really. One thing to keep in mind is logically where you want to catch the exception. Ideally, you want to catch it where you can handle it. That is, where your code can sufficiently recover from the error. If it's a fatal error and the application should stop entirely, then you can throw the exception further up the stack and let it go unhandled (though still logged where you caught it).

If, however, you're in a logical condition where you can just log the error and move on, then the catch block allows you to do just that. Log the details, update the state of any objects/data which need to be updated, and continue with the flow of the application.

Upvotes: 0

Kevin DiTraglia
Kevin DiTraglia

Reputation: 26058

Even if you aren't logging the problem, you can usually get the error in question from the event viewer within windows.

Upvotes: 0

Oded
Oded

Reputation: 498952

Have a global exception handler that writes the exception details to a file.

If you wrap the code in your Main method in a try{}catch{} block, you can write out the exception details in the catch block.

try
{
  // Calls to application code
}
catch(Exception ex)
{
   // log `ex.ToString()`
   throw; // rethrow to ensure termination optionally: `Application.Exit`
}

Upvotes: 0

Related Questions