抛出异常,并且捕获后恢复执行代码 [英] Resuming execution of code after exception is thrown and caught

查看:187
本文介绍了抛出异常,并且捕获后恢复执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这怎么可能继续代码执行异常被抛出之后,

How is it possible to resume code execution after an exception is thrown?

例如,采取以下代码:

namespace ConsoleApplication1
{
    public class Test
    {
        public void s()
        {
            throw new NotSupportedException();
            string @class = "" ;
            Console.WriteLine(@class);
            Console.ReadLine();
        }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                new Test().s();
            }
            catch (ArgumentException x)
            {
            }
            catch (Exception ex)
            {
            }
        }
    }
}

醒目通过步进当异常后,程序将停止运行。我怎么还进行执行

After catching the exception when stepping through, the program will stop running. How can I still carry on execution?

编辑:我特别的意思是行Console.WriteLine(@class);?似乎并没有受到较大冲击,因为当我运行它时,在调试模式下,程序从调试模式退出。我想运行到这一行,并在它停下来。

What I specifically mean is the line Console.WriteLine(@class); does not seem to be hit, because when I run to it when in debug mode, the program exits from debug mode. I want to run to this line and stop at it.

感谢

推荐答案

好了,你不用块中的抓后的任何代码,所以程序会停止运行。不知道你想做什么。

Well, you don't have any code after the catch blocks, so the program would stop running. Not sure what you're trying to do.

下面列出的是证明该计划并不是简单地停后的块。如果有要执行的代码将在抓后执行代码块:

The following should be proof that the program doesn't simply "stop" after the catch blocks. It will execute code after the catch blocks if there is code to be executed:

static void Main(string[] args)
{
    try
    {
        new Test().s();
    }
    catch (ArgumentException x)
    {
        Console.WriteLine("ArgumentException caught!");
    }
    catch (Exception ex) 
    { 
        Console.WriteLine("Exception caught!");
    }

    Console.WriteLine("I am some code that's running after the exception!");
}



中的代码将打印取决于被捕获的异常上相应的字符串。然后,它会打印我是例外!末后运行一些代码。

更新

在您的编辑,你问为什么 Console.WriteLine(@class); 似乎并不被打。其原因是,你的明确的在 S()方法的第一行抛出异常;任何如下被忽略。当遇到异常,停止执行和异常被传播调用堆栈,直到相应的处理程序可以处理它(这可能是一个块对应于尝试一个包装有问题的声明同样的方法中,也可能是一个进一步阻碍了调用堆栈。如果没有找到合适的处理程序,该程序将堆栈跟踪终止[至少在Java中 - 不知道,如果同样在C#中发生的。)

In your edit you asked why Console.WriteLine(@class); does not seem to be hit. The reason is that you are explicitly throwing an exception in the very first line of your s() method; anything that follows is ignored. When an exception is encountered, execution stops and the exception is propagated up the call stack until the appropriate handler can handle it (this may be a catch block that corresponds to the try that wraps the statement in question within the same method, or it may be a catch block further up the call-stack. If no appropriate handler is found, the program will terminate with a stacktrace [at least in Java - not sure if the same happens in C#]).

如果你想按 Console.WriteLine 行,那么你不应该明确在方法的开头抛出异常。

If you want to hit the Console.WriteLine line, then you shouldn't be explicitly throwing an exception at the beginning of the method.

这篇关于抛出异常,并且捕获后恢复执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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