【机器视觉】 HDevelop语言基础(六)-错误处理

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 error
endif
* continue with program
...

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

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

global tuple ErrorCode
dev_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 code
ErrorCode := Exception[0]
* react to error
endtry
* program continues normally
...

04. HDevelop错误码

21000 HALCON operator error
21001 User defined exception (’throw’)
21002 User defined error during execution
21003 User defined operator does not implement execution interface
21010 HALCON license error
21011 HALCON startup error
21012 HALCON operator error
21020 Format error: file is not a valid HDevelop program or procedure
21021 File is no HDevelop program or has the wrong version
21022 Protected procedure could not be decompressed
21023 Protected procedure could not be compressed and encrypted for saving
21024 Format error: file is not a valid HDevelop program
21025 Format error: file is not a valid HDevelop procedure
21026 Format error: file is not a valid HDevelop procedure library
21030 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 statements
21033 Renaming of procedure failed
21034 Locked procedures are not supported for the selected action.
21034 Password protection/locked procedures
21035 Parallel execution statements, iconic assignments, or iconic comparisons
21035 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 variables
21040 Unable to open file
21041 Unable to read from file
21042 Unable to write to file
21043 Unable to rename file
21044 Unable to open file: invalid file name
21050 For this operator the parallel execution with par_start is not supported
21051 Thread creation failed
21052 Thread creation failed: exceeded maximum number of subthreads
21060 Iconic variable is not instantiated
21061 Control variable is not instantiated (no value)
21062 Wrong number of control values
21063 Wrong value type of control parameter
21064 Wrong value of control parameter
21065 Control parameter does not contain a variable
21066 Control parameter must be a constant value
21067 Wrong number of control values in condition variable
21068 Wrong type: Condition variable must be an integer or boolean
21070 Variable names must not be empty
21071 Variable names must not start with a number
21072 Invalid variable name
21073 Invalid name for a control variable: the name is already used for an iconic variable
21074 Invalid name for an iconic variable: the name is already used for a control variable
21075 An iconic variable is used in the wrong context: a control variable or a vector is expected
21076 A control variable is used in the wrong context: an iconic variable or a vector is expected
21077 An iconic vector variable is used in the wrong context: a control variable or a single value
is expected
21078 A control vector variable is used in the wrong context: an iconic variable or a single value
is expected
21080 For loop variable must be a number
21081 Step parameter of for loop must be a number
21082 End parameter of for loop must be a number
21083 Variable names must not be a reserved expression
21084 Case label value has already appeared in switch block
21085 Default label has already appeared in switch block
21086 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 defined
21091 Access to an unknown global variable
21092 Access to an invalid global variable
21093 Invalid name for a global variable: the name is already used for a procedure parameter
21100 Access to an erroneous expression
21101 Wrong index in expression list
21102 Empty expression
21103 Empty expression argument
21104 Syntax error in expression
21105 Too few function arguments in expression
21106 Too many function arguments in expression
21107 The expression has no return value
21108 The expression has the wrong type
21110 The expression has the wrong type: iconic expression expected
21112 The expression has the wrong type: control expression expected
21114 The expression has the wrong vector dimension
21116 Vector expression expected
21118 Single or tuple value expression expected instead of a vector
21120 Expression expected
21121 lvalue expression expected
21122 Variable expected
21123 Unary expression expected
21124 Expression list expected
21125 Function arguments in parentheses expected
21126 One function argument in parentheses expected
21127 Two function arguments in parentheses expected
21128 Three function arguments in parentheses expected
21129 Four function arguments in parentheses expected
21130 Five function arguments in parentheses expected
21131 Right parenthesis ’)’ expected
21132 Right curly brace ’}’ expected
21133 Right square bracket ’]’ expected
21134 Unmatched right parenthesis ’)’ found
21135 Unmatched right curly brace ’}’ found
21136 Unmatched right square bracket ’]’ found
21137 Second bar ’|’ expected
21138 Function name expected before parentheses
21139 Unterminated string detected
21140 Invalid character in an expression identifier detected
21141 Parameter expression expected
21142 Parameter expression is not executable    
21143 Vector method after . expected
21144 Vector method ’at’ after . expected
21145 Modifying vector methods are not allowed within parameters
21200 Syntax error in operator expression
21201 Identifier (operator or variable name) expected
21202 Syntax error in parameter list
21204 Parenthesis expected
21205 No parenthesis expected
21206 List of parameters in parenthesis expected
21207 Wrong number of parameters
21208 Unexpected characters at end of line
21209 Assign operator ’:=’ expected
21210 Expression after assign operator ’:=’ expected
21211 Expression in brackets ’[ ]for the assign_at index expected
21212 In for statement, after keyword ’by’ expression for parameter ’Step’ expected
21213 In for statement, after keyword ’to’ expression for parameter ’End’ expected
21214 In for statement, after assign operation (:=) expression for parameter ’Start’ expected
21215 In for statement, after ’for .. := .. to ..’ keyword ’by’ expected
21216 In for statement, after ’for .. := ..’ keyword ’to’ expected
21217 In for statement, assign operation ’:=for initializing the index variable expected
21218 After ’for’ keyword, assignment of ’Index’ parameter expected
21219 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’ keyword
21221 In for statement, error after assignment operation (:=) in expression of parameter ’Start’
or the following ’to’ keyword
21222 In for statement, invalid variable name in parameter ’Index’ or error in the following assign-
ment operation (:=)
21223 for statement not complete
21224 In for statement, space after ’for’ expected
21225 In for statement, space after ’to’ expected
21226 In for statement, space after ’by’ expected
21227 Wrong type: The switch statement requires an integer value as parameter
21228 Wrong type: The case statement requires a constant integer value as parameter
21229 At the end of the case and the default statement a colon is expected
21230 Unknown operator or procedure
21231 Qualifier ’par_start’ before ’:’ or ’<ThreadID>’ expected
21232<ThreadID>’ variable in angle brackets after ’par_start’ expected
21233 ThreadID variable after ’par_start<’ expected
21234 Closing angle bracket (>) after ’par_start<ThreadID’ expected
21235 Colon (:) after ’par_start<ThreadID>’ expected
21236 Operator or procedure call after ’par_start :’ expected
21900 Internal value has the wrong type
21901 Internal value is not a vector
21902 Index into internal value is out of range
21903 Internal value is not instantiated
22000 Internal operation in expression failed
22001 Internal operation in a constant expression failed
22010 Parameters are tuples with different size
22011 Division by zero
22012 String exceeds maximum length
22100 Parameter is an empty tuple
22101 Parameter has more than one single value
22102 Parameter is not a single value
22103 Parameter has the wrong number of elements
22104 Parameter contains undefined value(s)
22105 Parameter contains wrong value(s)
22106 Parameter contains value(s) with the wrong type
22200 First parameter is an empty tuple
22201 First parameter has more than one single value
22202 First parameter is not a single value
22203 First parameter has the wrong number of elements
22204 First parameter contains undefined value(s)
22205 First parameter contains wrong value(s)
22206 First parameter contains value(s) with the wrong type
22300 Second parameter is an empty tuple
22301 Second parameter has more than one single value
22302 Second parameter is not a single value
22303 Second parameter has the wrong number of elements
22304 Second parameter contains undefined value(s)
22305 Second parameter contains wrong value(s)
22306 Second parameter contains value(s) with the wrong type
22400 Calling context was not set
22401 Accessing an invalid calling context
22402 Error while accessing calling context data
22500 Communication with external application failed
22501 Debug session with external application no longer valid
22502 An unexpected error occured in the external application
22503 Wrong password to unlock procedure in external application
23100 The generic parameter value is unknown
23101 The generic parameter name is unknown
30000 User defined exception    

05. HDevelop 元组操作总结

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

06. 附录

6.1 机器视觉博客汇总
网址:https://dengjin.blog.csdn.net/article/details/116837497

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值