为 JScript 实现错误处理。
try { [tryStatements] } catch(exception) { [catchStatements] } finally { [finallyStatements]}
参数
-
tryStatements
- 可选。可能发生错误的语句。 exception
- 必选。任何变量名称。 exception 的初始值是引发的错误的值。 catchStatements
- 可选。处理在相关联的 tryStatement 中发生的错误的语句。 finallyStatements
- 可选。在所有其他的错误过程发生之后被无条件执行的语句。
备注
try...catch...finally 语句提供了一种方法,可处理给定代码块中可能会发生的一些或全部错误,同时仍保持代码的运行。如果发生了程序员没有处理的错误,JScript 只给用户提供它的一般错误信息,就好象没有错误处理一样。
tryStatements 参数包含可能发生错误的代码,而 catchStatement 则包含了可处理任何发生的错误的代码。如果在 tryStatements 中发生了一个错误,则将把程序控制传递给 catchStatements 来处理该错误。exception 的初始值是发生在 tryStatements 中发生的错误的值。如果不发生错误,则不执行 catchStatements。
如果在与发生错误的 tryStatements 相关联的 catchStatements 中不能处理该错误,则使用 throw 语句将这个错误传播或重新引发给更高级的错误处理程序。
在执行完 tryStatements 中的语句,并在 catchStatements 的所有错误处理发生之后,可无条件执行 finallyStatements 中的语句。
请注意,即使 try 或 catch 块中出现返回语句,或 catch 块中引发错误,都会执行 finallyStatements 中的代码。finallyStatments 一定会始终运行。
示例
下面的示例阐释了 JScript 异常处理是如何进行的。
try
{ print("Outer try running...");try
{ print("Nested try running..."); throw "an error"; }catch
(e) { print("Nested catch caught " + e); throw e + " re-thrown"; }finally
{ print("Nested finally is running..."); } }catch
(e) { print("Outer catch caught " + e); }finally
{ print("Outer finally running"); }
将生成以下输出:
Outer try running.. Nested try running... Nested catch caught an error Nested finally is running... Outer catch caught an error re-thrown Outer finally running
在 catch 块中可以使用 throw 语句再次引发已由 catch 语句捕获的异常。例如:
catch (InvalidCastException e)
{
throw (e); // Rethrowing exception e
}
如果要再次引发当前由无参数的 catch 子句处理的异常,则使用不带参数的 throw 语句。例如:
catch
{
throw;
}C# 程序员参考
try-catch请参见
C# 关键字 | 与 C++ 比较 | 异常处理语句 | throw | try-finally | 引发异常 | C. 语法
try-catch 语句由一个 try 块和其后所跟的一个或多个 catch 子句(为不同的异常指定处理程序)构成。此语句会采用下列形式之一:
try try-block
catch (exception-declaration-1) catch-block-1
catch (exception-declaration-2) catch-block-2
...
try try-block catch catch-block
其中:
try-block
包含应引发异常的代码段。
exception-declaration, exception-declaration-1, exception-declaration-2
异常对象声明。
catch-block, catch-block-1, catch-block-2
包含异常处理程序。
备注
try-block 包含可能导致异常的保护代码块。该块一直执行到引发异常或成功完成为止。例如,下列转换 null 对象的尝试引发 NullReferenceException 异常:
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 块外部使用此变量时将生成编译器错误:使用了未赋值的局部变量。
public static void Main()
{
int x;
try
{
x = 123; // Don't do that.
// ...
}
catch
{
// ...
}
Console.Write(x); // Error: Use of unassigned local variable 'x'.
}
有关 catch 的更多信息,请参见 try-catch-finally。
示例
在此例中,try 块包含对可能导致异常的 MyFn() 方法的调用。catch 子句包含仅在屏幕上显示消息的异常处理程序。当从 MyFn() 内部调用 throw 语句时,系统查找 catch 语句并显示 Exception caught 消息。
// Rethrowing exceptions:
using System;
class MyClass
{
public static void Main()
{
MyClass x = new MyClass();
try
{
string s = null;
x.MyFn(s);
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
}
public void MyFn(string s)
{
if (s == null)
throw(new ArgumentNullException());
}
}
输出
发生以下异常:
System.ArgumentNullException
示例
此例使用了两个 catch 语句。最先出现的最特定的异常被捕获。
// Ordering catch clauses
using System;
class MyClass
{
public static void Main()
{
MyClass x = new MyClass();
try
{
string s = null;
x.MyFn(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);
}
}
public void MyFn(string s)
{
if (s == null)
throw new ArgumentNullException();
}
}
输出
发生以下异常:
System.ArgumentNullException
在前面的示例中,如果从特定程度最小的 catch 子句开始,您将收到此错误信息:
A previous catch clause already catches all exceptions of this or a super type ('System.Exception')
但是,若要捕获特定程度最小的异常,请使用下面的语句替换 throw 语句:
throw new Exception();