我正在研究我的 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 (据我所知),它只是一个信息点,允许您执行日志记录和此类任务。
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/