1

i'm trying to do globalexceptionhandling, i've already tried 3 methods to do this but nothing caught an exception thrown somewhere in the program

    static void Main()
    {
        System.Windows.Forms.Application.EnableVisualStyles();
        System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);

        System.Windows.Forms.Application.ThreadException +=
            new System.Threading.ThreadExceptionEventHandler(Catch);

        AppDomain.CurrentDomain.UnhandledException +=
            new UnhandledExceptionEventHandler(Catch);

        try
        {
            Application.Instance.Start();
        }
        catch (Exception ex)
        {
            StackTrace st = new StackTrace(ex, true);
            StackFrame[] frames = st.GetFrames();

            List<string> errorList = new List<string>();
            IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());


            foreach (var frame in frames)
            {
                errorList.Add("PC-Name: " + System.Environment.MachineName + "\nIP: " + host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork) + "\nUser-Name: " + Application.Instance.LoggedInTester.LastName + " " + Application.Instance.LoggedInTester.FirstName + "\nDateiname: " + frame.GetFileName() + "\nMethode: " + frame.GetMethod().Name + "\nZeile: " + frame.GetFileLineNumber() + "\n\n");
            }

            SyslogMessage msg = new SyslogMessage(DateTime.Now, Facility.SecurityOrAuthorizationMessages1, Severity.Warning, host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString(), "Prüfmittelüberwachung", errorList.FirstOrDefault());

            SyslogUdpSender sender = new SyslogUdpSender("localhost", 514);
            sender.Send(new SyslogMessage(DateTime.Now, Facility.SecurityOrAuthorizationMessages1, Severity.Warning, host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString(), "Prüfmittelüberwachung", errorList.FirstOrDefault()), new SyslogRfc3164MessageSerializer());
        }


    }

    static void Catch(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        StackTrace st = new StackTrace(e.Exception, true);
        StackFrame[] frames = st.GetFrames();

        List<string> errorList = new List<string>();
        IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());


        foreach (var frame in frames)
        {
            errorList.Add("PC-Name: " + System.Environment.MachineName + "\nIP: " + host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork) + "\nUser-Name: " + Application.Instance.LoggedInTester.LastName + " " + Application.Instance.LoggedInTester.FirstName + "\nDateiname: " + frame.GetFileName() + "\nMethode: " + frame.GetMethod().Name + "\nZeile: " + frame.GetFileLineNumber() + "\n\n");
        }

        SyslogMessage msg = new SyslogMessage(DateTime.Now, Facility.SecurityOrAuthorizationMessages1, Severity.Warning, host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString(), "Prüfmittelüberwachung", errorList.FirstOrDefault());

        SyslogUdpSender send = new SyslogUdpSender("localhost", 514);
        send.Send(new SyslogMessage(DateTime.Now, Facility.SecurityOrAuthorizationMessages1, Severity.Warning, host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString(), "Prüfmittelüberwachung", errorList.FirstOrDefault()), new SyslogRfc3164MessageSerializer());

    }

    static void Catch(object sender, UnhandledExceptionEventArgs e)
    {
        StackTrace st = new StackTrace((Exception)e.ExceptionObject,                                                                                true);
        StackFrame[] frames = st.GetFrames();

        List<string> errorList = new List<string>();
        IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());


        foreach (var frame in frames)
        {
            errorList.Add("PC-Name: " + System.Environment.MachineName + "\nIP: " + host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork) + "\nUser-Name: " + Application.Instance.LoggedInTester.LastName + " " + Application.Instance.LoggedInTester.FirstName + "\nDateiname: " + frame.GetFileName() + "\nMethode: " + frame.GetMethod().Name + "\nZeile: " + frame.GetFileLineNumber() + "\n\n");
        }

        SyslogMessage msg = new SyslogMessage(DateTime.Now, Facility.SecurityOrAuthorizationMessages1, Severity.Warning, host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString(), "Prüfmittelüberwachung", errorList.FirstOrDefault());

        SyslogUdpSender send = new SyslogUdpSender("localhost", 514);
        send.Send(new SyslogMessage(DateTime.Now, Facility.SecurityOrAuthorizationMessages1, Severity.Warning, host.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork).ToString(), "Prüfmittelüberwachung", errorList.FirstOrDefault()), new SyslogRfc3164MessageSerializer());

    }

this is what i've already tried, nothing caught the exception.

and i'm not using the standard application.run method, i'm using a singleton class to start from, where for every view(form) i have a presenter gets created in which the view gets created

does anyone know how to do globalexception handling with this setup?

also, sorry for my bad english

best regards

EDIT: MVCE

namespace Application
{
static class Program
{
    static void Main()
    {
        System.Windows.Forms.Application.EnableVisualStyles();
        System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);

        System.Windows.Forms.Application.ThreadException +=
            new System.Threading.ThreadExceptionEventHandler(Catch);

        AppDomain.CurrentDomain.UnhandledException +=
            new UnhandledExceptionEventHandler(Catch);

        try
        {
            Application.Instance.Start();
        }
        catch (Exception ex)
        {
            //do some catching
        }


    }

    static void Catch(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        //do some catching
    }

    static void Catch(object sender, UnhandledExceptionEventArgs e)
    {
        //do some catching
    }
}

public class Application
{
    private static Application _instance;

    private Application()
    {

    }

    public static Application Instance
    {
        get
        {
            return _instance ?? (_instance = new Application());
        }
    }

    internal void Start()
    {
        StartOverviewWindow();
    }

    private void StartOverviewWindow()
    {
        throw new Exception();
    }
}

}

Claus
  • 53
  • 9
  • "i'm using a singleton class to start from, where for every view(form) i have a presenter gets created in which the view gets created" - that's what application.instance is, the Application.Instance.Start() starts the first presenter which starts the first view – Claus Oct 05 '17 at 09:20
  • *"an exception thrown somewhere"* - can you create an [MCVE](https://stackoverflow.com/help/mcve)? Are you talking about [task unhandled exception](https://stackoverflow.com/q/21648094/1997232)? You can [edit](https://stackoverflow.com/posts/46581941/edit) your question to add missing information. – Sinatr Oct 05 '17 at 09:22
  • i just did a "throw new Exception()" somewhere in the program so i know if it works or not, thats what i meant, i'll do a MCVE – Claus Oct 05 '17 at 09:23
  • One thing that seems to be missing is `Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);` – stuartd Oct 05 '17 at 09:35
  • tried that as well, error gets still not caught – Claus Oct 05 '17 at 09:36
  • It is working. Since you `throw` in `Application.Instance.Start();` the `try/catch` will get it. Add `MessageBox.Show("Tada");` (or breakpoint) in `catch` of your mcve to see. – Sinatr Oct 05 '17 at 09:37
  • tried as well now, the mbo works, but does anyone of you know why i cant set a breakpoint in that catch function? – Claus Oct 05 '17 at 09:43

1 Answers1

0

As a quick answer (because I can't find duplicates for all of those), to handle

an exception thrown somewhere

you have to handle following events:

Application.ThreadException += ...

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += ...

// tasks exceptions, add in app.config:
//  <runtime>
//      <ThrowUnobservedTaskExceptions enabled="true"/>
//  </runtime>
TaskScheduler.UnobservedTaskException += ...

For difference between Application.ThreadException and AppDomain.CurrentDomain.UnhandledException see this.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • as you can see, i'm already using ThreadException and UnhandledException, and they are not working, but thanks. – Claus Oct 05 '17 at 09:34
  • Thank you, "TaskScheduler.UnobservedTaskException" was the solution for my problem. – Alois Jun 15 '21 at 07:24