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

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

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

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

throwT_throwThrowThrowthrow抛出(算子)

名称

throwT_throwThrowThrowthrow — 抛出用户定义的异常或重新抛出捕获的异常。

签名

throw( : : Exception : )

Herror T_throw(const Htuple Exception)

void Throw(const HTuple& Exception)

static void HOperatorSet.Throw(HTuple exception)

def throw(exception: Sequence[Union[int, str]]) -> None

描述

借助 trytryTryTryTrytrycatchcatchCatchCatchCatchcatchendtryendtryEndtryEndtryEndtryendtrythrowthrowThrowThrowThrowthrow 算子,可在 HDevelop 中实现动态异常处理,其功能与 C++ 和 C# 中的异常处理机制相当。HDevelop 异常处理的基本概念亦在 trytryTryTryTrytry 算子、dev_set_checkdev_set_checkDevSetCheckDevSetCheckDevSetCheckdev_set_check 算子以及 “HDevelop用户指南” 中有所阐述。

算子 throwthrowThrowThrowThrowthrow 提供了从程序任意位置抛出异常的机会。该异常可被外围 trytryTryTryTrytry-catchcatchCatchCatchCatchcatch 代码块的 catchcatchCatchCatchCatchcatch 算子捕获。通过这种方式,开发者能够定义特定的错误或异常状态,此时常规程序执行将终止,转而执行特定的跨函数异常处理流程——例如释放资源或从预定义状态重启程序。

在这种用户自定义异常中,几乎任意元组都可以作为 ExceptionExceptionExceptionExceptionexceptionexception 参数抛出,只需将元组的首个元素设置为 ≥30000 的用户自定义错误代码即可。

此外,借助 throwthrowThrowThrowThrowthrow 算子,可以重新抛出由 catchcatchCatchCatchCatchcatch 算子捕获的异常。这种做法在某些情况下是合理的,例如当在内部 trytryTryTryTrytry-catchcatchCatchCatchCatchcatch-endtryendtryEndtryEndtryEndtryendtry 代码块(如外部过程函数)中,仅能妥善处理特定异常,而所有其他异常必须传递给调用方,以便由外部 trytryTryTryTrytry-catchcatchCatchCatchCatchcatch-endtryendtryEndtryEndtryEndtryendtry 代码块进行捕获和处理。

对于重新抛出捕获到的异常,可以将由 catchcatchCatchCatchCatchcatch 算子捕获的 ExceptionExceptionExceptionExceptionexceptionexception 元组直接传递给 throwthrowThrowThrowThrowthrow 算子的 ExceptionExceptionExceptionExceptionexceptionexception 参数。此外,还可以在 ExceptionExceptionExceptionExceptionexceptionexception 元组中附加任意(但是不是图标)用户数据,这些数据在捕获异常后可通过 dev_get_exception_datadev_get_exception_dataDevGetExceptionDataDevGetExceptionDataDevGetExceptionDatadev_get_exception_data 算子以 'user_data'"user_data""user_data""user_data""user_data""user_data" 形式访问:
trytryTryTryTrytry
...
catch(Exception)catch(Exception)Catch(Exception)Catch(Exception)Catch(Exception)catch(Exception)
...
UserData := ...
throw([Exception, UserData])throw([Exception, UserData])Throw([Exception, UserData])Throw([Exception, UserData])Throw([Exception, UserData])throw([Exception, UserData])
endtryendtryEndtryEndtryEndtryendtry

注意

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

参数

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

元组返回异常数据或用户定义的错误代码。

结果

如果指定参数的值正确,throwthrowThrowThrowThrowthrow(作为算子)返回 2 ( H_MSG_TRUE )。否则,将抛出一个异常并返回错误代码。

另见

trytryTryTryTrytry, catchcatchCatchCatchCatchcatch, endtryendtryEndtryEndtryEndtryendtry, dev_get_exception_datadev_get_exception_dataDevGetExceptionDataDevGetExceptionDataDevGetExceptionDatadev_get_exception_data, dev_set_checkdev_set_checkDevSetCheckDevSetCheckDevSetCheckdev_set_check

模块

基础