开发者

Catching C# unhandled exceptions from other DLLs

开发者 https://www.devze.com 2023-04-11 01:57 出处:网络
I have to split up parts of my app into dlls. I have an unhandled exception event in my Application main, and I\'m throwing exceptions in my other DLL to test an exception dump process.

I have to split up parts of my app into dlls.

I have an unhandled exception event in my Application main, and I'm throwing exceptions in my other DLL to test an exception dump process.

Problem is that it can catch custom exceptions ONLY when they are thrown within a constructor.

I'm assuming maybe my custom exception is being wrapped by a system one, but it couldn't catch divide by zero either (again, testing).

What could I have done wrong?

In the code below. NONE of my handlers are actually ever called. The app terminates silently.

MAIN.DLL

    static void开发者_JAVA技巧 Main()
    {
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Application_UnhandledException);
        Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
        Application.Run(new Form1());
    }

    public static void Application_UnhandledException(
        object sender, UnhandledExceptionEventArgs e)
    {

        MessageBox.Show(e.ExceptionObject.ToString());

    }

    public static void Application_ThreadException(
object sender, ThreadExceptionEventArgs e)
    {

        MessageBox.Show(e.Exception.ToString());

    }

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        dcp.Init();
    }

    public static DefaultCommandProcessor.Dcp dcp = new DefaultCommandProcessor.Dcp();
}

OTHER DLL

    public partial class Dcp : Form
    {
        public Dcp()
        {

            int y = 0;
            int x = 4 / y;  // caught in unhandled exception event in app.
        }
        public Int32 Init()
        {

            int y = 0;
            int x = 4 / y; // not caught
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消