我如何prevent和/或处理一个StackOverflowException? [英] How do I prevent and/or handle a StackOverflowException?

查看:175
本文介绍了我如何prevent和/或处理一个StackOverflowException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想无论是prevent或处理,我从内XSL编辑我写一个调用XslCompiledTransform.Transform方法获得一个StackOverflowException。这个问题似乎是用户可以编写一个XSL脚本,是无限递归,和它只是吹了在调用变换方法。 (即,该问题不只是典型的编程错误,这通常是这样的异常的原因。)

I would like to either prevent or handle a StackOverflowException that I am getting from a call to the XslCompiledTransform.Transform method within an Xsl Editor I am writing. The problem seems to be that the user can write an Xsl script that is infinitely recursive, and it just blows up on the call to the Transform method. (That is, the problem is not just the typical programmatic error, which is usually the cause of such an exception.)

有没有一种方法来检测和/或限制多少递归被允许?或者有什么其他的想法,以保持该code从刚吹了我的电话?

Is there a way to detect and/or limit how many recursions are allowed? Or any other ideas to keep this code from just blowing up on me?

推荐答案

微软:

与.NET Framework开始   2.0版,一个StackOverflowException   对象不能被捕获一个try-catch   块和相应的过程是   默认情况下终止。因此,   建议用户写自己的code   检测和prevent堆叠   溢出。例如,如果您   应用程序依赖于递归,使用   计数器或状态条件   终止递归循环。

Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default. Consequently, users are advised to write their code to detect and prevent a stack overflow. For example, if your application depends on recursion, use a counter or a state condition to terminate the recursive loop.

我假设异常发生一个内部.NET方法中,而不是在你的code。

I'm assuming the exception is happening within an internal .NET method, and not in your code.

您可以做两件事情。

  • 在写code,检查的XSL无限递归和之前应用变换(唉)通知用户。
  • 加载的XslTransform code到单独的进程(哈克,但不工作)。

您可以使用Process类加载,将应用转换成一个单独的进程大会,如果它死了,没有杀死你的主要的应用程序提醒失败的用户。

You can use the Process class to load the assembly that will apply the transform into a separate process, and alert the user of the failure if it dies, without killing your main app.

编辑:我只是测试,这里是如何做到这一点:

I just tested, here is how to do it:

MainProcess:

MainProcess:

// 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过程:

ApplyTransform Process:

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);
        }
    }
}

这篇关于我如何prevent和/或处理一个StackOverflowException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆