我有抛出异常后,打破? [英] Do I have to break after throwing exception?

查看:173
本文介绍了我有抛出异常后,打破?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C#中的自定义类,我抛出了几个例外,如果人们给了错误的输入中的一些方法。如果抛出异常,将所有的code的方法后抛出仍执行?我必须把断线抛出之后,还是一抛永远退出方式?

I'm writing a custom class in C# and I'm throwing a couple exceptions if people give the wrong inputs in some of the methods. If the exception is thrown, will any of the code in the method after the throw still be executed? Do I have to put a break after the throw, or does a throw always quit the method?

推荐答案

在您的抛出异常,要得到执行下一个code是覆盖的方法内抛出任何catch块(如果有的话),那么,在的finally方框的(如果有的话)。你可以试试,一个try-catch,一个try-catch-最后还是一个try-finally。然后,如果不处理的异常,重新抛出的catch块还是不抓根本,控制返回给调用者。例如,你会得到支持1,是2,是3从这个code ...

When you throw an exception, the next code to get executed is any catch block that covers that throw within the method (if any) then, the finally block (if any). You can have a try, a try-catch, a try-catch-finally or a try-finally. Then, if the exception is not handled, re-thrown by a catch block or not caught at all, control is returned to the caller. For example, you will get "Yes1, Yes2, Yes3" from this code ...

try
{
    Console.WriteLine("Yes1");
    throw (new Exception());
    Console.WriteLine("No1");

}
catch
{
    Console.WriteLine("Yes2");
    throw;
    Console.WriteLine("No2");
}
finally
{
    Console.WriteLine("Yes3");
}

Console.WriteLine("No3");

这篇关于我有抛出异常后,打破?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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