c# - AppDomain 不应该引起的异常?

标签 c# .net appdomain

我正在研究我的 MCTS,目前正在研究 AppDomain 功能。但我遇到了一些不清楚的事情。 AppDomain 应该捕获 Exception 并允许域安全卸载。 (除了别处建议的 StackOverflowException 可能的异常(exception))

AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
Evidence adevidence = AppDomain.CurrentDomain.Evidence;
domain.UnhandledException += new UnhandledExceptionEventHandler(domain_UnhandledException);
AppDomain domain = AppDomain.CreateDomain("MyDomain", adevidence, setup);

当我决定在为此目的创建的程序集中创建示例类的实例时,我应该得到一个安全、受限的域,它将捕获发生的错误并可以安全地卸载。至少我从学习书上是这样理解的。

var type = (IDoSomeWork) domain.CreateInstanceAndUnwrap("Library1", "Library1.Class1");
type.Run();

这会在 type.Run() 上引发异常(因为我是那样做的)。但是 AppDomain 不应该安全地捕获它吗?这不是我们有 AppDomain 的原因吗?

更新:

根据要求,我已经包含了 Library1.Class1 的定义。此外,为清楚起见,UnhandledExceptionEventHandler 对捕获异常没有影响,并且与问题无关。

[Serializable]
public class Class1 : MarshalByRefObject, IDoSomeWork
{
    public void Run()
    {
        Debug.WriteLine(AppDomain.CurrentDomain.FriendlyName);
        throw new ApplicationException(String.Format("{0}", this.ToString()));
    }
}

我已验证它在 MyDomain 中运行。

最佳答案

UnhandledException 事件不会捕获传统意义上的异常,如 try-catch block (据我所知),它只是一个信息点,允许您执行日志记录和此类任务。

引自MSDN documentation :

This event provides notification of uncaught exceptions. It allows the application to log information about the exception before the system default handler reports the exception to the user and terminates the application.

所以异常仍然会冒泡到系统默认的异常处理程序。

关于c# - AppDomain 不应该引起的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1710497/

相关文章:

c# - 我应该使用多个嵌套的 "using"语句吗?

c# - 为数字符号编写正则表达式

.net - 覆盖 GetHashCode 的最佳算法是什么?

c# - IEnumerable/IEnumerable<T> 接口(interface)只有 MoveNext 的原因是什么?

c# - 在 .NET 4.0 中,如何 'sandbox' 一个内存程序集并执行一个方法?

c# - FileSystemWatcher - 从 ObservableCollection 添加和删除,C#

c# - 高频堆

c# - 从 .Net 的角度看 Windows 服务的体系结构

c# - 如何从不同应用程序域中的类库调用实例方法?

.net - 如何加载 .NET 程序集以进行反射操作并随后卸载它?