Skip to content

Latest commit

 

History

History
39 lines (25 loc) · 1.98 KB

5.18.trycatch.md

File metadata and controls

39 lines (25 loc) · 1.98 KB

5.18.try/catch

Haxe允许捕获值,使用 try/catch 语法:

Haxe allows catching values using its try/catch syntax:

try try-expr 
catch(varName1:Type1) catch-expr-1 
catch(varName2:Type2) catch-expr-2 

如果在运行时try表达式引发一个 throw(第5.22节),它可以被任何后续的 catch块捕捉到。这些块由下面部分组成:

If during runtime the evaluation of try-expression causes a throw(5.22),it can be caught by any subsequent catch block. These blocks consist of

  • 一个变量名用来保存被抛出的值
  • 一个显式的类型注释,决定捕捉哪种类型的值
  • 这种情况下要执行的表达式
  • a variable name which holds the thrown value,
  • an explicit type annotation which determines which types of values to catch, and
  • the expression to execute in that case.

Haxe 允许抛出和捕捉任何类型的值,它不限于继承自一个特定的异常或者错误类的类型。catch块从上至下检查,第一个和抛出的值类型兼容的被采用。

Haxe allows throwing and catching any kind of value, it is not limited to types inheriting from a specific exception or error class. Catch blocks are checked from top to bottom with the first one whose type is compatible with the thrown value being picked.

这个过程有许多和编译时的合一(第3.5节)行为的相似之处。然而,因为检查必须在运行时进行,所以有几个限制:

This process has many similarities to the compile-time unification (3.5) behavior. However, since the check has to be done at runtime there are several restrictions:

  • 类型必须在运行时存在:类实例(第2.3节),enum实例(第2.4节),抽象核心类型(第2.8.7节)和 动态类型(第2.7节)。
  • 类型参数必须只能为 Dynamic(第2.7节)。
  • The type must exist at runtime: Class instances (2.3), enum instances (2.4), abstract core types (2.8.7) and Dynamic (2.7).
  • Type parameters can only be Dynamic (2.7).