ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – AppDomain未处理的异常

2019-06-25 10:02:42  阅读:199  来源: 互联网

标签:c exception-handling appdomain


有很多主题涉及这个问题.但是我有一个问题.

我将程序集加载到新的AppDomain中,如下所示:

public void Run()
{
    //There's the problem.
    //As Panos Rontogiannis mentioned the thread is created in default AppDomain
    new Thread(RunApp).Start();
}

private void RunApp()
    try
    {
        AppDomain.CreateDomain("domain name").ExecuteAssembly("path to assembly");
    }
    catch (Exception _e)
    {
        MessageBox.Show("Unhandled Exception.\n" + _e);
    }
}

在加载程序集的Main方法中,我将处理程序订阅到UnhandledException事件:

AppDomain.CurrentDomain.UnhandledException += handleException;

处理程序本身:

public static void handleException(object a_s, UnhandledExceptionEventArgs a_args)
{
    var _e = (Exception)a_args.ExceptionObject;
    //Static loger class method
    Loger.WriteError(_e.GetType().ToString(), _e.Message, "default solution");
}

但是无论何处在加载的程序集中抛出异常,处理程序都不会涉及.我只在默认的AppDomain中捕获异常(第一次尝试{} catch {}).

解决方法:

最有可能的是,您无法在新AppDomain中处理异常的原因是它不会从该AppDomain中创建的线程抛出.从AppDomain.UnhandledException的文档来看,这不是很直截了当.有趣的部分如下:

An exception is unhandled only if the entire stack for the thread has
been unwound without finding an applicable exception handler, so the
first place the event can be raised is in the application domain where
the thread originated.

现在,如果执行抛出代码的线程在主AppDomain中创建(如控制台应用程序的主线程),那么您应该在主AppDomain中添加一个处理程序.请注意,如果主AppDomain中未加载抛出异常的类型,.NET的Assembly Loader将尝试从应用程序的基目录和探测路径加载它.如果这些与子AppDomain不同,则不会解析程序集并抛出(其他)异常.

标签:c,exception-handling,appdomain
来源: https://codeday.me/bug/20190625/1285301.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有