try-catch 语句由一个 try 块后跟一个或多个 catch 子句构成,这些子句指定不同的异常处理程序。
备注
try 块包含可能导致异常的保护代码。该块一直执行到引发异常或成功完成为止。例如,下列强制转换 null 对象的尝试引发
![]() | |
---|---|
object o2 = null; try { int i2 = (int)o2; // Error } |
catch 子句使用时可以不带任何参数,这种情况下它捕获任何类型的异常,并被称为一般 catch 子句。它还可以接受从 System.Exception 派生的对象参数,这种情况下它处理特定的异常。例如:
![]() | |
---|---|
catch (InvalidCastException e) { } |
在同一个 try-catch 语句中可以使用一个以上的特定 catch 子句。这种情况下 catch 子句的顺序很重要,因为会按顺序检查 catch 子句。将先捕获特定程度较高的异常,而不是特定程度较小的异常。
在 catch 块中可以使用 throw 语句再次引发已由 catch 语句捕获的异常。例如:
![]() | |
---|---|
catch (InvalidCastException e) { throw (e); // Rethrowing exception e } |
如果要再次引发当前由无参数的 catch 子句处理的异常,则使用不带参数的 throw 语句。例如:
![]() | |
---|---|
catch { throw; } |
在 try 块内部时应该只初始化其中声明的变量;否则,完成该块的执行前可能发生异常。例如,在下面的代码示例中,变量 x
在 try 块内初始化。试图在 Write(x)
语句中的 try 块外部使用此变量时将产生编译器错误:
![]() | |
---|---|
static void Main() { int x; try { // Don't initialize this variable here. x = 123; } catch { } // Error: Use of unassigned local variable 'x'. Console.Write(x); } |
有关 catch 块的更多信息,请参见 try-catch-finally。
示例
在此例中,try 块包含对可能导致异常的 MyMethod()
方法的调用。catch 子句包含仅在屏幕上显示消息的异常处理程序。当从 MyMethod
内部调用 throw 语句时,系统查找 catch 语句并显示 Exception caught
消息。
![]() | |
---|---|
// try_catch_example.cs using System; class MainClass { static void ProcessString(string s) { if (s == null) { throw new ArgumentNullException(); } } static void Main() { try { string s = null; ProcessString(s); } catch (Exception e) { Console.WriteLine("{0} Exception caught.", e); } } } |
示例输出
System.ArgumentNullException: Value cannot be null. at MainClass.Main() Exception caught. |
此例使用了两个 catch 语句。最先出现的最特定的异常被捕获。
![]() | |
---|---|
// try_catch_ordering_catch_clauses.cs using System; class MainClass { static void ProcessString(string s) { if (s == null) { throw new ArgumentNullException(); } } static void Main() { try { string s = null; ProcessString(s); } // Most specific: catch (ArgumentNullException e) { Console.WriteLine("{0} First exception caught.", e); } // Least specific: catch (Exception e) { Console.WriteLine("{0} Second exception caught.", e); } } } |
示例输出
System.ArgumentNullException: Value cannot be null. at MainClass.Main() First exception caught. |
注释
在前面的示例中,如果从具体程度最低的 catch 子句开始,您将收到以下错误信息:A previous catch clause already catches all exceptions of this or a super type ('System.Exception')
。
但是,若要捕获特定程度最小的异常,请使用下面的语句替换 throw 语句:
throw new Exception()
;
C# 语言规范
有关更多信息,请参见 C# 语言规范中的以下各章节:
-
5.3.3.13 Try-catch 语句
-
8.10 try 语句
-
16 异常
请参见
任务
参考
C# 关键字异常处理语句(C# 参考)
throw(C# 参考)
try-finally(C# 参考)
概念
C# 编程指南其他资源
C# 参考