栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何防止和/或处理StackOverflowException?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何防止和/或处理StackOverflowException?

从Microsoft:

从.NET framework 2.0版开始,try-
catch块无法捕获StackOverflowException对象,并且默认情况下终止了相应的进程。因此,建议用户编写其代码以检测并防止堆栈溢出。例如,如果您的应用程序依赖于递归,请使用计数器或状态条件终止递归循环。

我假设异常发生在内部.NET方法中,而不是您的代码中。

您可以做几件事。

  • 编写代码,检查xsl的无限递归并在应用变换(Ugh)之前通知用户。
  • 将XslTransform代码加载到一个单独的进程中(麻烦,但工作较少)。

您可以使用Process类来加载将转换应用到单独流程中的程序集,并在失败后向用户警告失败,而不会终止您的主应用程序。

编辑:我刚刚测试,这是怎么做的:

主流程:

// This is just an example, obviously you'll want to pass args to this.Process p1 = new Process();p1.StartInfo.FileName = "ApplyTransform.exe";p1.StartInfo.UseShellExecute = false;p1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;p1.Start();p1.WaitForExit();if (p1.ExitCode == 1)       Console.WriteLine("StackOverflow was thrown");

ApplyTransform流程:

class Program{    static void Main(string[] args)    {        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);        throw new StackOverflowException();    }    // We trap this, we can't save the process,     // but we can prevent the "ILLEGAL OPERATION" window     static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)    {        if (e.IsTerminating)        { Environment.Exit(1);        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/391663.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号