使用右上角的选项卡切换到不同的编程语言。

使用右上角的选项卡切换到不同的编程语言。

使用右上角的选项卡切换到不同的编程语言。

使用右上角的选项卡切换到不同的编程语言。

catchT_catchCatchCatchcatch捕获(算子)

名称

catchT_catchCatchCatchcatch — 捕获在前面的 trytryTryTryTrytry 块中抛出的异常。

签名

catch( : : : Exception)

Herror T_catch(Htuple* Exception)

void Catch(HTuple* Exception)

static void HOperatorSet.Catch(out HTuple exception)

def catch() -> Sequence[Union[int, str]]

描述

借助 trytryTryTryTrytrycatchcatchCatchCatchCatchcatchendtryendtryEndtryEndtryEndtryendtrythrowthrowThrowThrowThrowthrow 算子,可在 HDevelop 中实现动态异常处理,其功能与 C++ 和 C# 中的异常处理机制相当。HDevelop 异常处理的基本概念已在 trytryTryTryTrytrythrowthrowThrowThrowThrowthrowdev_set_checkdev_set_checkDevSetCheckDevSetCheckDevSetCheckdev_set_check 算子说明以及 “HDevelop 用户指南” 中进行阐述。

算子 catchcatchCatchCatchCatchcatch 结束一段被监视的程序代码块,并启动一段在错误情况下必须执行的程序代码块。若 trytryTryTryTrytry-catchcatchCatchCatchCatchcatch 代码块在未发生异常时被执行,则 catchcatchCatchCatchCatchcatch-endtryendtryEndtryEndtryEndtryendtry 代码块将被忽略,程序执行将在对应的 endtryendtryEndtryEndtryEndtryendtry 算子之后继续。相反,在错误情况下,程序执行将直接从发生错误的算子(或 throwthrowThrowThrowThrowthrow 算子)跳转至周围 trytryTryTryTrytry-catchcatchCatchCatchCatchcatch 代码块的 catchcatchCatchCatchCatchcatch 算子。输出控制参数 ExceptionExceptionExceptionExceptionexceptionexception 在算子出错时返回包含预定义数据的元组,用于描述错误详情。若异常由 throwthrowThrowThrowThrowthrow 算子抛出,则可返回任意用户自定义元组。

ExceptionExceptionExceptionExceptionexceptionexception 元组中最关键的数据是错误代码。因此,该代码作为 ExceptionExceptionExceptionExceptionexceptionexception 元组的首项传递,可通过 'Exception'"Exception""Exception""Exception""Exception""Exception"[0] 直接访问。但其余所有数据必须通过 dev_get_exception_datadev_get_exception_dataDevGetExceptionDataDevGetExceptionDataDevGetExceptionDatadev_get_exception_data 算子获取,因为提供的数据顺序和范围可能在未来版本中变更,且不同编程语言导出时可能存在差异。尤其需要注意的是,在导出代码中,错误元组的部分项不可用,另一些项(如错误消息)可能需在请求时才能确定。

若异常由算子错误、HALCON 错误代码(< 10000)引发,或中止的算子属于扩展包,则返回用户自定义错误代码(> 10000)作为错误代码。所有 HALCON 错误代码列表详见“扩展包程序员手册”附录。由算子 throwthrowThrowThrowThrowthrow 抛出的用户自定义 ExceptionExceptionExceptionExceptionexceptionexception 元组的首个元素应为 ≥30000 的错误代码。其余元组元素可自由选择,不受任何限制。

如果在 HDevelop 或 HDevEngine 中发生算子错误,则由 ExceptionExceptionExceptionExceptionexceptionexception 元组提供以下错误信息:

在大多数情况下,使用 HALCON 错误代码即可实现自动异常处理。附加数据主要用于向 HDevelop 程序开发者提供有关错误状态的信息,以供调试之用。注意:在导出的代码中,通常无法获取错误位置的相关信息。

注意

C 语言不支持导出 trytryTryTryTrytrycatchcatchCatchCatchCatchcatchendtryendtryEndtryEndtryEndtryendtrythrowthrowThrowThrowThrowthrow 算子,仅C++、C# 和 VisualBasic/.NET 语言支持。其中仅后者支持跨函数抛出异常。

参数

ExceptionExceptionExceptionExceptionexceptionexception (输出控制)  exception-array HTupleSequence[Union[int, str]]HTupleHtuple (integer / string) (int / long / string) (Hlong / HString) (Hlong / char*)

返回异常数据的元组。

结果

catchcatchCatchCatchCatchcatch 始终返回 2 ( H_MSG_TRUE )。

可能的后继

dev_get_exception_datadev_get_exception_dataDevGetExceptionDataDevGetExceptionDataDevGetExceptionDatadev_get_exception_data

另见

trytryTryTryTrytry, endtryendtryEndtryEndtryEndtryendtry, throwthrowThrowThrowThrowthrow, dev_get_exception_datadev_get_exception_dataDevGetExceptionDataDevGetExceptionDataDevGetExceptionDatadev_get_exception_data, dev_set_checkdev_set_checkDevSetCheckDevSetCheckDevSetCheckdev_set_check

模块

基础