【机器视觉】 catch算子
00. 目錄
文章目錄
- 00. 目錄
- 01. 概述
- 02. 簽名
- 03. 描述
- 04. 注意
- 05. 參數
- 06. 結果
- 07. 附錄
01. 概述
catch - 捕獲在前面的try塊中拋出的異常。
02. 簽名
catch( : : : Exception)
03. 描述
使用算子try,catch,endtry和throw可以在HDevelop中實現動態的異常處理,這相當于C ++和C#中的異常處理。 HDevelop中異常處理的基本概念在算子try,throw和dev_set_check以及“HDevelop用戶指南”中進行了描述。
算子catch在錯誤情況時結束一個監控的程序行塊,并跳轉至另一個程序行塊。 如果try-catch塊執行沒有出現異常,則catch-endtry塊將被忽略,程序將在相應的endtry算子后繼續執行。 相反,在錯誤情況下,程序執行從發生錯誤的算子(或從throw算子)直接跳轉到try-catch塊附近的catch算子。 輸出控制參數Exception返回一個元組,包含一組預定義的數據,用于描述發生算子錯誤時的錯誤。 如果throw運算符拋出異常,則可以返回任意的用戶定義的元組。
Exception元組中最重要的數據是錯誤代碼。 因此,它作為Exception元組的第一項,可以通過’Exception’[0]直接訪問。 但是,所有其他數據都必須通過算子dev_get_exception_data進行訪問,因為提供的數據的順序和范圍在將來的版本中可能會更改,并且可能因不同的編程語言導出而有所不同。 尤其是,必須考慮到,在導出的代碼中有一些錯誤元組的詳情是不可用的,有些詳情在請求之前可能無法確定(如錯誤消息)。
如果由算子錯誤引發異常,HALCON錯誤代碼<10000。如果從擴展庫算子引發的異常,則會返回用戶定義的錯誤代碼(> 10000)作為錯誤代碼。 所有HALCON錯誤代碼的列表可以在’Extension Package Programmer’s Manual’的附錄中找到。 算子throw的用戶定義的Exception元組的第一個元素應該是一個錯誤代碼> = 30000,可以加入其他的元組元素,沒有任何限制。
如果在算子在HDevelop或HDevEngine中發生錯誤,Exception元組將提供以下關于錯誤的附加信息:
HALCON錯誤消息。
附加的HDevelop特定錯誤代碼,用于指定在HALCON算子(代碼= 21000)內還是在算子外部(例如,在評估和分配參數表達式期間)捕獲到錯誤。 在后一種情況下,錯誤代碼更精確地指定了錯誤的類型。
一個確切的特定HDevelop錯誤消息。
發生錯誤的程序行號。
拋出異常的算子名稱(如果在受保護的程序中拋出異常,則返回’ - protected - '而不是算子名稱)。
調用堆棧的深度(如果錯誤發生在“main”,則返回深度1)。
在大多數情況下,對于自動異常處理,使用HALCON錯誤代碼就足夠了。 其他數據主要是為了向HDevelop程序的開發者提供一些關于錯誤情況的信息,以便于調試。 注意:在一般情況下,在導出的代碼中有關錯誤位置的信息將不可用。
原文描述:
With the help of the operators try, catch, endtry, and throw it is possible to implement a dynamic exception handling in HDevelop, which is comparable to the exception handling in C++ and C#. The basic concepts of the exception handling in HDevelop are described at the operators try, throw, and dev_set_check as well as in the “HDevelop User’s Guide”.
The operator catch ends a block of watched program lines and starts a block of program lines that have to be executed in an error case. If the try-catch block is executed without an exception, the catch-endtry block is ignored and program execution continues after the corresponding endtry operator. In contrast, in an error case the program execution jumps directly from the operator where the error occurred (or from the throw operator) to the catch operator of the surrounding try-catch block. The output control parameter Exception returns a tuple that contains a predefined set of data describing the error in case an operator error occurred. If the exception was thrown by the throw operator, an arbitrary user-defined tuple can be returned.
The most important data within the Exception tuple is the error code. Therefore, this is passed as the first item of the Exception tuple and can be accessed directly with ‘Exception’[0]. However, all other data has to be accessed through the operator dev_get_exception_data, because the order and the extent of the provided data may change in future versions and may vary for different programming language exports. Especially, it has to be taken into account that in the exported code there are some items of the error tuple that are not available and others that might not be determined until they are requested (like error messages).
If the exception was thrown by an operator error, a HALCON error code (< 10000) or if the aborted operator belongs to an extension package, a user-defined error code (> 10000) is returned as the error code. A list of all HALCON error codes can be found in the appendix of the “Extension Package Programmer’s Manual”. The first element of a user-defined Exception tuple thrown by the operator throw should be an error code >= 30000. Additional tuple elements can be chosen without any restrictions.
If an operator error occurred within HDevelop or HDevEngine, the following information about the error is provided by the Exception tuple:
The HALCON error code.
An additional HDevelop specific error code that specifies whether an error was caught within the HALCON operator (code = 21000) or outside the operator, e.g., during the evaluation and assignment of the parameter expressions. In the latter case the error code specifies the kind of error more precisely.
The HALCON error message.
An appropriate HDevelop-specific error message.
The number of the program line, where the error occurred.
The name of the operator that threw the exception (if the exception was thrown in a protected procedure, ‘–protected–’ is returned instead of the operator name).
The depth of the call stack (if the error occurred in ‘main’ a depth of 1 is returned).
The name of the procedure, where the error occurred.
In most cases, for an automatic exception handling it is sufficient to use the HALCON error code. Additional data is primarily passed in order to provide some information about the error condition to the developer of the HDevelop program for debugging reasons. Attention: in the exported code, in general, information about the error location will not be available.
04. 注意
算子try,catch,endtry和throw不支持C語言導出,支持語言C ++,C#和VisualBasic / .NET。 只有后者支持跨程序拋出異常。
05. 參數
Exception (output_control) exception-array → (integer / string)
返回異常數據的元組。
06. 結果
catch總是返回2(H_MSG_TRUE)。
HDevelop例程
try_catch.hdev Demonstrate the usage of the exception handling in HDevelop
set_shape_model_timeout.hdev Demonstrate how to use the timeout mechanism for shape-based matching
cancel_draw_result.hdev Enable user-defined actions when a draw operation is canceled
程序示例
07. 附錄
7.1 機器視覺博客匯總
網址:https://dengjin.blog.csdn.net/article/details/116837497
總結
以上是生活随笔為你收集整理的【机器视觉】 catch算子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【机器视觉】 case算子
- 下一篇: 【机器视觉】 continue算子