抛出异常后是否需要返回(c++ 和 c#)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16854308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
do I need a return after throwing exception (c++ and c#)
提问by mans
I have a function that generate an exception. For example the following code:
我有一个生成异常的函数。例如下面的代码:
void test()
{
ifstream test("c:/afile.txt");
if(!test)
{
throw exception("can not open file");
}
// other section of code that reads from file.
}
Do I need a return after throwing the exception?
抛出异常后是否需要返回?
What is the case in c#?
在 c# 中是什么情况?
采纳答案by Aasmund Eldhuset
throwusually causes the function to terminate immediately, so you even if you do put any code after it (inside the same block), it won't execute. This goes for both C++ and C#. However, if you throw an exception inside a tryblock and the exception gets caught, execution will continue in the appropriate catchblock, and if there is a finallyblock (C# only), it will be executed whether an exception is thrown or not. At any rate, any code immediately after the throwwill never be executed.
throw通常会导致函数立即终止,所以即使你在它之后(在同一个块内)放了任何代码,它也不会执行。这适用于 C++ 和 C#。但是,如果在try块内抛出异常并且异常被捕获,则将在适当的catch块中继续执行,并且如果存在finally块(仅限 C#),则无论是否抛出异常,都会执行该块。无论如何,紧跟在 之后的任何代码throw都不会被执行。
(Note that having a throwdirectly inside a try/catchis usually a design problem - exceptions are designed for bubbling errors up across functions, not for error handling within a function.)
(请注意,throw直接在try/内部使用catch通常是一个设计问题 - 异常旨在跨函数冒泡错误,而不是用于函数内的错误处理。)
回答by Belogix
After you call throwthe method will return immediately and no code following it will be executed. This is also true if any exceptions are thrown and not caught in a try / catchblock.
调用throw该方法后,该方法将立即返回,并且不会执行其后的任何代码。如果任何异常被抛出并且没有在try / catch块中被捕获,这也是正确的。
回答by Oscar
No, you don't need to return, because after the exception is thrown the code after that wont' be executed.
不,你不需要返回,因为在抛出异常之后,之后的代码不会被执行。
回答by user2417992
If it's a void method you will never need a return instruction.
如果它是一个 void 方法,您将永远不需要返回指令。
Then you can't put anything after the throw instruction it will never be used if something is throw
然后你不能在 throw 指令之后放任何东西,如果有东西被抛出,它将永远不会被使用
void test()
{
ifstream test("c:/afile.txt");
if(!test)
{
throw exception("can not open file");
// If there is code here it will never be reach !
}
// other section of code that reads from file.
//if you place code here it will be reach only if you don"t throw an exception, so only if test == true in your case
}
回答by Quinton Bernhardt
Strictly speaking throwing will NOT necessarily terminate the function immediately always.... as in this case,
严格来说,抛出不一定总是立即终止函数......就像在这种情况下,
try {
throw new ApplicationException();
} catch (ApplicationException ex) {
// if not re-thrown, function will continue as normal after the try/catch block
} catch (Exception ex) {
}
and then there is the Finally block - but after that it will exit.
然后是 finally 块 - 但在那之后它将退出。
So no, you do not have to return.
所以不,你不必返回。

