【机器视觉】 HDevelop语言基础(六)-错误处理
发布日期:2021-06-29 20:52:46 浏览次数:2 分类:技术文章

本文共 10793 字,大约阅读时间需要 35 分钟。

00. 目录

文章目录

01. 概述

本节介绍如何在 HDevelop 程序中处理错误。 发生错误时,默认行为HDevelop 的作用是停止程序执行并显示错误消息框。 虽然这在程序开发时肯定是有益的,但在程序实际部署时通常并不需要。 一个完成的程序应该对错误本身做出反应。 如果程序与用户交互,这尤其重要。

HDevelop 中的错误处理基本上有两种方法:

• 跟踪算子调用的返回值(错误代码)
• 使用异常处理

这些方法之间的主要区别在于应用领域:第一种方法在发生错误的过程中处理错误。 后一种方法允许错误在调用堆栈中向上工作,直到最终得到处理。

02. 跟踪算子的返回值

算子 dev_set_check 指定是否显示错误消息框。

要关闭消息框,请使用

dev_set_check('~give_error')

HDevelop 然后将忽略程序中的任何错误。 因此,程序员必须负责错误处理。 每个操作符调用都提供一个返回值(或错误代码),表示执行成功或失败。 可以通过指定的错误变量访问此错误代码:

dev_error_var(ErrorCode, 1)

此算子调用实例化变量 ErrorCode。 它存储最后执行的运算符的错误代码。 使用此错误代码,程序可以根据操作的成功来确定其进一步的流程。

...if (ErrorCode != H_MSG_TRUE)* react to errorendif* continue with program...

可以使用算子 get_error_text 获取与给定错误代码相关的错误消息。 这在向程序用户报告错误时很有用。

如果要在调用过程中处理错误,则必须在每个参与过程的接口中添加适当的输出控制变量,或者必须将错误变量定义为全局变量。

global tuple ErrorCodedev_error_var(ErrorCode, 1)...

03. 异常处理

HDevelop 支持动态异常处理,类似于 C++ 和 C# 中的异常处理。

监视程序行块是否存在运行时错误。 如果发生错误,则会引发异常并调用关联的异常处理程序。 异常处理程序只是另一个程序行块,除非发生错误,否则它对程序流是不可见的。 异常处理程序可以直接对错误采取行动,也可以将相关信息(即异常)传递给父异常处理程序。 这也称为重新抛出异常。

与上一节中描述的跟踪方法相反,异常处理需要将 HDevelop 设置为在出现错误时停止。 这是默认行为。 它也可以显式打开:

dev_set_check('give_error')

此外,HDevelop 可以配置为让用户选择是否抛出异常,或者自动抛出异常。 此行为在首选项选项卡 General Options -> Experienced User的用户中设置。

HDevelop 异常是包含与特定错误相关的数据的元组。 它始终包含错误代码作为第一项。 算子dev_get_exception_data 提供对异常元组元素的访问。

HDevelop 异常处理的应用方式如下:

...try* start block of watched program lines...catch(Exception)* get error codeErrorCode := Exception[0]* react to errorendtry* program continues normally...

04. HDevelop错误码

21000 HALCON operator error21001 User defined exception (’throw’)21002 User defined error during execution21003 User defined operator does not implement execution interface21010 HALCON license error21011 HALCON startup error21012 HALCON operator error21020 Format error: file is not a valid HDevelop program or procedure21021 File is no HDevelop program or has the wrong version21022 Protected procedure could not be decompressed21023 Protected procedure could not be compressed and encrypted for saving21024 Format error: file is not a valid HDevelop program21025 Format error: file is not a valid HDevelop procedure21026 Format error: file is not a valid HDevelop procedure library21030 The program was modified inconsistently outside HDevelop.21031 The program was modified outside HDevelop: inconsistent procedure lines.21032 The program was modified outside HDevelop: unmatched control statements21033 Renaming of procedure failed21034 Locked procedures are not supported for the selected action.21034 Password protection/locked procedures21035 Parallel execution statements, iconic assignments, or iconic comparisons21035 Procedures with advanced language elements are not supported for the selected action.21036 Procedures with vector variables are not supported for the selected action.21036 Vector variables21040 Unable to open file21041 Unable to read from file21042 Unable to write to file21043 Unable to rename file21044 Unable to open file: invalid file name21050 For this operator the parallel execution with par_start is not supported21051 Thread creation failed21052 Thread creation failed: exceeded maximum number of subthreads21060 Iconic variable is not instantiated21061 Control variable is not instantiated (no value)21062 Wrong number of control values21063 Wrong value type of control parameter21064 Wrong value of control parameter21065 Control parameter does not contain a variable21066 Control parameter must be a constant value21067 Wrong number of control values in condition variable21068 Wrong type: Condition variable must be an integer or boolean21070 Variable names must not be empty21071 Variable names must not start with a number21072 Invalid variable name21073 Invalid name for a control variable: the name is already used for an iconic variable21074 Invalid name for an iconic variable: the name is already used for a control variable21075 An iconic variable is used in the wrong context: a control variable or a vector is expected21076 A control variable is used in the wrong context: an iconic variable or a vector is expected21077 An iconic vector variable is used in the wrong context: a control variable or a single valueis expected21078 A control vector variable is used in the wrong context: an iconic variable or a single valueis expected21080 For loop variable must be a number21081 Step parameter of for loop must be a number21082 End parameter of for loop must be a number21083 Variable names must not be a reserved expression21084 Case label value has already appeared in switch block21085 Default label has already appeared in switch block21086 The type of the variable could not be determined (no proper type definition found)21087 The type of the variable could not be determined (conflicting type definitions found)21090 A global variable with the specified name but a different type is already defined21091 Access to an unknown global variable21092 Access to an invalid global variable21093 Invalid name for a global variable: the name is already used for a procedure parameter21100 Access to an erroneous expression21101 Wrong index in expression list21102 Empty expression21103 Empty expression argument21104 Syntax error in expression21105 Too few function arguments in expression21106 Too many function arguments in expression21107 The expression has no return value21108 The expression has the wrong type21110 The expression has the wrong type: iconic expression expected21112 The expression has the wrong type: control expression expected21114 The expression has the wrong vector dimension21116 Vector expression expected21118 Single or tuple value expression expected instead of a vector21120 Expression expected21121 lvalue expression expected21122 Variable expected21123 Unary expression expected21124 Expression list expected21125 Function arguments in parentheses expected21126 One function argument in parentheses expected21127 Two function arguments in parentheses expected21128 Three function arguments in parentheses expected21129 Four function arguments in parentheses expected21130 Five function arguments in parentheses expected21131 Right parenthesis ’)’ expected21132 Right curly brace ’}’ expected21133 Right square bracket ’]’ expected21134 Unmatched right parenthesis ’)’ found21135 Unmatched right curly brace ’}’ found21136 Unmatched right square bracket ’]’ found21137 Second bar ’|’ expected21138 Function name expected before parentheses21139 Unterminated string detected21140 Invalid character in an expression identifier detected21141 Parameter expression expected21142 Parameter expression is not executable    21143 Vector method after . expected21144 Vector method ’at’ after . expected21145 Modifying vector methods are not allowed within parameters21200 Syntax error in operator expression21201 Identifier (operator or variable name) expected21202 Syntax error in parameter list21204 Parenthesis expected21205 No parenthesis expected21206 List of parameters in parenthesis expected21207 Wrong number of parameters21208 Unexpected characters at end of line21209 Assign operator ’:=’ expected21210 Expression after assign operator ’:=’ expected21211 Expression in brackets ’[ ]’ for the assign_at index expected21212 In for statement, after keyword ’by’ expression for parameter ’Step’ expected21213 In for statement, after keyword ’to’ expression for parameter ’End’ expected21214 In for statement, after assign operation (’:=’) expression for parameter ’Start’ expected21215 In for statement, after ’for .. := .. to ..’ keyword ’by’ expected21216 In for statement, after ’for .. := ..’ keyword ’to’ expected21217 In for statement, assign operation ’:=’ for initializing the index variable expected21218 After ’for’ keyword, assignment of ’Index’ parameter expected21219 In for statement, error after ’by’ keyword in expression of parameter ’Step’21220 In for statement, error after ’to’ keyword in expression of parameter ’End’ or the following’by’ keyword21221 In for statement, error after assignment operation (’:=’) in expression of parameter ’Start’or the following ’to’ keyword21222 In for statement, invalid variable name in parameter ’Index’ or error in the following assign-ment operation (’:=’)21223 for statement not complete21224 In for statement, space after ’for’ expected21225 In for statement, space after ’to’ expected21226 In for statement, space after ’by’ expected21227 Wrong type: The switch statement requires an integer value as parameter21228 Wrong type: The case statement requires a constant integer value as parameter21229 At the end of the case and the default statement a colon is expected21230 Unknown operator or procedure21231 Qualifier ’par_start’ before ’:’ or ’
’ expected21232 ’
’ variable in angle brackets after ’par_start’ expected21233 ThreadID variable after ’par_start<’ expected21234 Closing angle bracket (’>’) after ’par_start
’ expected21236 Operator or procedure call after ’par_start :’ expected21900 Internal value has the wrong type21901 Internal value is not a vector21902 Index into internal value is out of range21903 Internal value is not instantiated22000 Internal operation in expression failed22001 Internal operation in a constant expression failed22010 Parameters are tuples with different size22011 Division by zero22012 String exceeds maximum length22100 Parameter is an empty tuple22101 Parameter has more than one single value22102 Parameter is not a single value22103 Parameter has the wrong number of elements22104 Parameter contains undefined value(s)22105 Parameter contains wrong value(s)22106 Parameter contains value(s) with the wrong type22200 First parameter is an empty tuple22201 First parameter has more than one single value22202 First parameter is not a single value22203 First parameter has the wrong number of elements22204 First parameter contains undefined value(s)22205 First parameter contains wrong value(s)22206 First parameter contains value(s) with the wrong type22300 Second parameter is an empty tuple22301 Second parameter has more than one single value22302 Second parameter is not a single value22303 Second parameter has the wrong number of elements22304 Second parameter contains undefined value(s)22305 Second parameter contains wrong value(s)22306 Second parameter contains value(s) with the wrong type22400 Calling context was not set22401 Accessing an invalid calling context22402 Error while accessing calling context data22500 Communication with external application failed22501 Debug session with external application no longer valid22502 An unexpected error occured in the external application22503 Wrong password to unlock procedure in external application23100 The generic parameter value is unknown23101 The generic parameter name is unknown30000 User defined exception

05. HDevelop 元组操作总结

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

06. 附录

6.1 机器视觉博客汇总

网址:

转载地址:https://dengjin.blog.csdn.net/article/details/117917938 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:【机器视觉】dev_update_on和dev_update_off算子
下一篇:【机器视觉】 HDevelop语言基础(五)-多线程

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月27日 19时23分31秒