C# 执行多个 catch 块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12583487/
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
Execution of multiple catch blocks
提问by chandra sekhar
Possible Duplicate:
executing multiple catch blocks
可能的重复:
执行多个 catch 块
Can multiple catch blocks be executed for a single try statement?How can we handle the catch blocks?Can we create try without catch block??
一个try语句可以执行多个catch块吗?我们如何处理catch块?我们可以创建没有catch块的try吗?
回答by Prabhu Murthy
- You can have multiple catch blocks associated with a try block,but only a single catch block can ever handle your exception.
- Yes you can have a try block without a catch,but it is mandatory to have a finally block
- 您可以有多个与 try 块相关联的 catch 块,但只有一个 catch 块可以处理您的异常。
- 是的,你可以在没有 catch 的情况下使用 try 块,但必须有一个 finally 块
回答by Habib
Yes you can have multiple catch blocks with try statement. You start with catching specific exceptions and then in the last block you may catch base Exception. Only one of the catch block will handle your exception.
是的,你可以有多个带有 try 语句的 catch 块。您从捕获特定异常开始,然后在最后一个块中您可以捕获 base Exception。只有一个 catch 块会处理您的异常。
You can have try block without a catch block. try/finally will do.
你可以在没有 catch 块的情况下使用 try 块。尝试/终于会做。
try
{
Console.Write("test");
}
catch (IOException ex)
{
}
catch (ArithmeticException ex)
{
}
catch (Exception ex)
{
}
try without catch
尝试不捕获
try
{
}
finally
{
}
回答by Aghilas Yakoub
1 Yes it's possible to have multiple catch, one catch for one specific exception
1 是的it's possible to have multiple catch,一个特定异常的捕获
Sample
样本
try
{
...
}
catch (FormatException)
{
....
}
catch (OverflowException)
{
...
}
2 You can have tryinstrction without catch
2 你可以没有try指令catch
try
{
...
}
Finally
{
.....
}
回答by CrazyCasta
As for the second part of your question, you can do either
至于你问题的第二部分,你可以这样做
try
{
stuff...
}
catch(Exception ex){}
or
或者
try
{
stuff...
}
finally{}
, but you can't do try by itself.
,但你不能自己尝试。
回答by Lews Therin
Only one catchblock will execute, and it will be the catchthat closely matches the type of the Exceptionthat was thrown.
You can't have a tryby it self. You need a try catch (one or more catch block)or a try finallyor a try catch finally
只会catch执行一个块,并且它将是catch与Exception抛出的 类型紧密匹配的。你不能拥有try它自己。你需要一个try catch (one or more catch block)或一个try finally或一个try catch finally
回答by Science_Fiction
Technically you can only hit one catch BUT you say:
从技术上讲,你只能击中一个球,但你说:
Can multiple catch blocks be EXECUTED for a single try statement
可以为单个 try 语句执行多个 catch 块吗
Keyword being executed. So, you could try:
正在执行的关键字。所以,你可以试试:
catch (Exception ex)
{
if (ex is MyException1||
ex is MyException2)
{
//do stuff
}
else
{
throw;
}
}
回答by Kishore Kumar
You can have any number of catchblock for a single try..catch statement.
catch单个 try..catch 语句可以有任意数量的块。
But please remeber one thing, that your
但请记住一件事,你的
catch (Exception ex)
{
...
}
Should be the last catch block, since all exceptions inherits the the class Exception.
应该是最后一个 catch 块,因为所有异常都继承了类 Exception。
回答by Thorsten Dittmar
There can be multiple catch blocks (as said in other answers already), but only the one that first matches the exception type is executed. That means you need to order the catch blocks properly. For example:
可以有多个 catch 块(如其他答案中所述),但只执行第一个匹配异常类型的块。这意味着您需要正确订购 catch 块。例如:
try
{
}
catch (Exception exp1)
{
// Block 1
}
catch (IOException exp2)
{
// Block 2
}
Block 2 will never be executed, as block 1 catches every exception (all exception classes are derived from Exception).
块 2 永远不会被执行,因为块 1 会捕获每个异常(所有异常类都来自Exception)。
try
{
}
catch (IOException exp1)
{
// Block 1
}
catch (Exception exp2)
{
// Block 2
}
In this example, block 2 will only be executed if the exception is not an IOExceptionor derived from IOException. If an IOExceptionis thrown, only block 1 will execute, block 2 will not.
在此示例中,仅当异常不是IOException或派生自时才会执行块 2 IOException。如果IOException抛出an ,则只有块 1 会执行,块 2 不会。

