C# 对在C中抛出异常感到困惑吗#

C# 对在C中抛出异常感到困惑吗#,c#,exception,exception-handling,try-catch,C#,Exception,Exception Handling,Try Catch,我使用try/catch和throw来处理异常。因此,我使用try/catch捕获错误,包括文件不可用等问题,然后在文本包含错误值时使用throw myMain()的基本布局如下: while ((line = sr.ReadLine()) != null) { try { //get the input from readLine and saving it

我使用
try/catch
throw
来处理异常。因此,我使用
try/catch
捕获错误,包括文件不可用等问题,然后在
文本包含错误值时使用
throw

my
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 


            }

因此,如果您注意到我在catch语句中抛出了一个异常
try/catch
,并且该异常不会停止程序,而当尝试在catch语句中抛出
exception
时,代码会停止。有人知道如何解决这个问题吗?

我不知道你说的“停止程序”是什么意思。如果不处理异常,但代码正在处理通过catch(exception e)块抛出的异常,则程序将停止。
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; 

            }

或者您的意思是想退出while循环,在这种情况下,您可以使用break

我不确定“停止程序”是什么意思。如果不处理异常,但代码正在处理通过catch(exception e)块抛出的异常,则程序将停止。
或者您的意思是希望退出while循环,在这种情况下,您可以使用break

如果在
catch
中抛出异常,则该
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
}

如果在
catch
中抛出异常,则该
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
}

默认情况下,除非在catch块中重新抛出异常,否则该异常将在捕获它的“catch”块处停止向上传播。也就是说,程序不会退出

如果您不想捕获异常并希望程序退出,则有两个选项: -删除“异常”的catch块 -在其catch块中重新显示异常

catch (Exception e)
{
   throw e; // rethrow the exception, else it will stop propogating at this point
}
通常,除非您对异常有某种逻辑响应,否则请避免捕获它。这样,您就不会“隐藏”或抑制会导致程序出错的错误


此外,MSDN文档是理解异常处理的一个很好的地方:

默认情况下,除非在catch块中重新抛出异常,否则异常将在捕获它的“catch”块处停止向上传播。也就是说,程序不会退出

如果您不想捕获异常并希望程序退出,则有两个选项: -删除“异常”的catch块 -在其catch块中重新显示异常

catch (Exception e)
{
   throw e; // rethrow the exception, else it will stop propogating at this point
}
通常,除非您对异常有某种逻辑响应,否则请避免捕获它。这样,您就不会“隐藏”或抑制会导致程序出错的错误



此外,MSDN文档是理解异常处理的一个很好的地方:

Hi here,使用
throw e仍然会在那一行停止代码。即使这不能回答问题,但值得注意的是,您永远不应该捕获并重新引用这样的异常。如果您想重新抛出刚才捕获的相同异常,只需编写
throw在那里,使用
抛出e仍然会在那一行停止代码。即使这不能回答问题,但值得注意的是,您永远不应该捕获并重新引用这样的异常。如果您想重新抛出刚才捕获的相同异常,只需编写
throw您正在处理异常(捕获它),然后什么也不做,这意味着它将继续。如果是Main()并且要从catch块中抛出expection。。您计划在哪里处理抛出的异常。。?当然这会让你的应用程序崩溃——你正在处理异常(捕捉它),然后什么都不做,这意味着它会继续。如果是Main()并且要从catch块中抛出expection。。您计划在哪里处理抛出的异常。。?当然,这会让你的应用程序崩溃,然后问题是在
try
statement@User1204501问题是在
catch
语句中引发异常。您可以在
try
部分中抛出异常,也可以在
catch
部分中添加另一个
try
/
catch
。try-catch块用于处理异常。catch块可以是catch(异常e)中最基本的,它将捕获所有异常,因为所有异常都派生自基类型异常。您可以删除第二个catch块,而您的异常将不会得到处理。我猜问题是在
try
statement@User1204501问题是在
catch
语句中引发异常。您可以在
try
部分中抛出异常,也可以在
catch
部分中添加另一个
try
/
catch
。try-catch块用于处理异常。catch块可以是catch(异常e)中最基本的,它将捕获所有异常,因为所有异常都派生自基类型异常。您可以删除第二个catch块,您的异常将不会被处理。我认为这是有意义的!让我快点试试,然后再打给你,先生!哦,好的,那么我要做的是,
true或false
如果值有效,那么不确定
try
语句将如何工作=(@User1204501也许你可以更新你的问题来解释你想做什么..也许你正在使用
throw
,而其他方法会更好。不用担心,我认为这会解决问题=)会接受答案我认为这很有意义!让我尽快尝试,然后回复你,先生!哦,好的,那么我正在做的是,