简体   繁体   English

对在C#中引发异常感到困惑

[英]Confused about throwing Exceptions in C#

I using try/catch and throw to handle exceptions. 我使用try/catchthrow来处理异常。 So I am using try/catch is used capture errors which include problems like file is not available etc and then using throw when the text contains wrong values. 所以我正在使用try/catch捕获错误,其中包括诸如文件不可用等问题,然后在text包含错误值时使用throw

The basic layout of my Main() is as follows: 我的Main()的基本布局如下:

   while ((line = sr.ReadLine()) != null)
        {
            try
            {
                //get the input from readLine and saving it

                if (!valuesAreValid)
                {
                    //this doesnt make the code stop running
                    throw new Exception("This value is not wrong"); 

                 } else{
                 //write to file
                 }
            }
            catch (IndexOutOfRangeException)
            {
               //trying to throw the exception here but the code stops

            }

            catch (Exception e)
            {

               //trying to throw the exception here but the code stops 


            }

So if you notice I am throwing an exception inside try/catch and that doesnt stop the program whereas when trying to throw Exception inside the catch statement, the code stops. 因此,如果您注意到我 try/catch中引发了Exception ,但并没有停止程序,而当尝试在catch语句中引发Exception ,代码停止了。 Does anyone have any idea how to fix this? 有谁知道如何解决这个问题?

If you throw an exception inside a catch , it will not be handled by that catch . 如果你抛出一个内部异常catch ,它不会被通过处理catch If there's no catch further up, you'll get an unhandled exception. 如果没有catch进一步上涨,你会得到一个未处理的异常。

try {
    try {
        throw new Exception("example");
    } catch {
        throw new Exception("caught example, threw new exception");
    }
} catch {
    throw new Exception("caught second exception, throwing third!");
    // the above exception is unhandled, because there's no more catch statements
}

By default, unless you rethrow an exception in the catch block, the exception will stop propogating upwards at the 'catch' block where it was caught. 默认情况下,除非您在catch块中重新抛出异常,否则该异常将停止在被捕获的“ catch”块中向上传播。 Which means, the program will not exit. 这意味着程序将不会退出。

If you don't want to catch the exception, and want the program to exit, you have two options: - remove the catch block for 'Exception' - rethrow the exception inside it's catch block. 如果您不想捕获异常,并且希望程序退出,则有两个选择:-删除'Exception'的catch块-将异常重新放入其catch块中。

catch (Exception e)
{
   throw e; // rethrow the exception, else it will stop propogating at this point
}

In general, unless you have some logical response to an exception, avoid catching it at all. 通常,除非您对异常有某种逻辑上的响应,否则请不要完全捕获它。 This way you will not "hide" or suppress errors that should cause the program to error out. 这样,您将不会“隐藏”或抑制应该导致程序错误输出的错误。

Also, the MSDN documentation is a pretty good place to understand exception handling: http://msdn.microsoft.com/en-us/library/vstudio/ms229005%28v=vs.100%29.aspx 另外,MSDN文档也是了解异常处理的好地方: http : //msdn.microsoft.com/zh-cn/library/vstudio/ms229005%28v=vs.100%29.aspx

while ((line = sr.ReadLine()) != null)
        {
            try
            {
                //get the input from readLine and saving it

                if (!valuesAreValid)
                {
                    //this doesnt make the code stop running
                    throw new Exception("This value is not wrong"); 

                 } else{
                 //write to file
                 }
            }
            catch (IndexOutOfRangeException)
            {
               //trying to throw the exception here but the code stops

            }

            catch (Exception e)
            {

               //trying to throw the exception here but the code stops 
             throw e; 

            }

I'm not sure what you mean by "stop the program". 我不确定“停止程序”是什么意思。 The program will stop if you do not handle an exception, but your code is handling the Exception you throw via the catch (Exception e) block. 如果您不处理异常,则程序将停止,但是您的代码正在处理通过catch(Exception e)块引发的异常。 Or maybe you mean that you want to exit the while loop, in which case you could use break . 或者,也许您的意思是您想退出while循环,在这种情况下,您可以使用break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM