Simulating a fatal exception  
Author Message
_Quimbly_





PostPosted: Visual C# General, Simulating a fatal exception Top

I have a top-level exception handler for my WinForms app that I'd like to
test, but in order to do so (fully), I need generate/simulate a fatal
exception (i.e. an exception which is caught but can't be ignored and causes
the application to crash.)

Thread.CurrentThread.Abort() doesn't seem to be the right choice, does it
I'm only working with the main and UI threads.

Any thoughts

Here's my code inside program.cs:
[STAThread]
static void Main()
{
CustomExceptionHandler exHandler = new CustomExceptionHandler();
Application.ThreadException += new
ThreadExceptionEventHandler(exHandler.OnThreadException);
Application.Run(new frmController());
}

internal class CustomExceptionHandler
{
public void OnThreadException(object sender, ThreadExceptionEventArgs t)
{
// Log the exception
ExceptionLogger.LogException(t.Exception);

try
{
// Exit the program if the user clicks Abort.
DialogResult result = ShowThreadExceptionDialog(t.Exception);

if (result == DialogResult.Abort)
{
Application.Exit();
}
// Otherwise try to continue program execution
}
catch
{
// Fatal error, terminate program
try
{
MessageBox.Show("Fatal Error", "Fatal Error",
MessageBoxButtons.OK,
MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
}

private DialogResult ShowThreadExceptionDialog(Exception ex)
{
string errorMessage =
"Unhandled Exception: " + Environment.NewLine +
Environment.NewLine +
ex.Message + Environment.NewLine + Environment.NewLine +
ex.GetType() + Environment.NewLine + Environment.NewLine +
"Stack Trace" + Environment.NewLine +
ex.StackTrace;

return MessageBox.Show(errorMessage, "Application Error",
MessageBoxButtons.AbortRetryIgnore,
MessageBoxIcon.Stop);
}
} // End inner class



Visual C#14  
 
 
Peter Ritchie





PostPosted: Visual C# General, Simulating a fatal exception Top

Do you want to simulate the circumstances behind certain fatal exceptions Or just test if the exceptions are handled by your handler If it's the later, just use "throw": "throw new AccessViolationException("some message");" or "throw new OutOfMemoryException("some message");" or "throw new StackOverflowException("some other message");".

 
 
SteveDrake





PostPosted: Visual C# General, Simulating a fatal exception Top

Are you asking if you code will work if a new thread, eg not the main UI thread throws an exception.

Will your

Application.ThreadException += new
ThreadExceptionEventHandler(exHandler.OnThreadException);

 

This is my test applicaiton, it did the trick :

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ConsoleApplication1
{
    public class myForm  :Form
    {
        Random rand = new Random();
        Label beeper = new Label();

        public myForm ()
        {
            this.Controls.Add(beeper);
           
            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
            timer.Interval = 1000;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Enabled = true;
        }

        void timer_Tick(object sender, EventArgs e)
        {
            System.Diagnostics.Trace.WriteLine("Beep");
            beeper.Text = "Beep " + DateTime.Now;
           
            if (rand.Next(5) == 1)
            {
                throw new Exception("Bang");
            }  

        }  
    }
    class Program
    {
        static Random rand = new Random();
        static void Main(string[] args)
        {
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
            Application.Run(new myForm());
        }
        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            System.Windows.Forms.MessageBox.Show ("Error");
        }
    }
}


 
 
SteveDrake





PostPosted: Visual C# General, Simulating a fatal exception Top

My example will not work for all cases, checkout version 2, this does not work. But, for you situation it may be fine :

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ConsoleApplication1
{
public class myForm :Form
{
Random rand = new Random();
Label beeper = new Label();

public myForm ()
{
this.Controls.Add(beeper);

System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
timer.Enabled = true;
}

void timer_Tick(object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine("Beep");
beeper.Text = "Beep " + DateTime.Now;

if (rand.Next(5) == 1)
{
// DOES NOT ERROR IN THIS EXAMPLE throw new Exception("Bang");
}

}
}
class Program
{
static Random rand = new Random();
static void Main(string[] args)
{

Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

System.Threading.Timer timer = new System.Threading.Timer( beeper, null, new TimeSpan(0, 0, 0), new TimeSpan(0, 0, 1));

Application.Run(new myForm());
}
static void beeper(object state)
{
System.Console.WriteLine("Beep");
if (rand.Next(5) == 1)
{
// we go pop
throw new Exception("Bang");
}
}
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
System.Windows.Forms.MessageBox.Show ("Error");
}
}
}