Use the tabs on the upper right to switch to a different programming language.

Use the tabs on the upper right to switch to a different programming language.

Use the tabs on the upper right to switch to a different programming language.

Use the tabs on the upper right to switch to a different programming language.

trytryTryTrytry (Operator)

Name

trytryTryTrytry — Starts a program block where exceptions are detected and caught.

Signature

try( : : : )

Herror try()

Herror T_try()

void Try()

static void HOperatorSet.Try()

def try() -> None

Description

With the help of the operators trytryTryTrytry, catchcatchCatchCatchcatch, endtryendtryEndtryEndtryendtry, and throwthrowThrowThrowthrow it is possible to implement a dynamic exception handling in HDevelop, which is comparable to the exception handling in C++ and C#. By the operators trytryTryTrytry, catchcatchCatchCatchcatch, and endtryendtryEndtryEndtryendtry two code blocks are formed: the first one (trytryTryTrytry .. catchcatchCatchCatchcatch) contains the watched program lines that perform the normal program logic. The second block (catchcatchCatchCatchcatch .. endtryendtryEndtryEndtryendtry) contains the code that has is executed if an exception occurs.

The operator trytryTryTrytry enables the exception handling for the following program lines, i.e., the following code block up to the corresponding catchcatchCatchCatchcatch operator is watched for exceptions. If during the execution of the subsequent program lines an error or another exceptional state occurs, or if an exception is thrown explicitly by the operator throwthrowThrowThrowthrow, the trytryTryTrytry block is left immediately (or---depending on a user preference---after displaying an error message box) and the program execution continues at the corresponding catchcatchCatchCatchcatch operator. If the exception is thrown within a procedure that was called from the trytryTryTrytry block (directly or via other procedure calls), the procedure call and all intermediate procedure calls that are on the call stack above the trytryTryTrytry block are immediately aborted (or, if applicable, also after displaying an error message box).

Whether an error message box is displayed before the exception is thrown or not, is controlled by the HDevelop preference 'Suppress error message dialogs within try-catch blocks' that can be reached via Edit->Preferences->General Options->Experienced User. This message box also offers the opportunity to stop the program execution before the exception is thrown in order to edit the possibly erroneous operator call.

The program block that is watched for exceptions ends with the corresponding catchcatchCatchCatchcatch operator. If within the watched trytryTryTrytry block no exception occurred, the following catchcatchCatchCatchcatch block is ignored and the program execution continues after the corresponding endtryendtryEndtryEndtryendtry operator.

trytryTryTrytry-catchcatchCatchCatchcatch-endtryendtryEndtryEndtryendtry blocks can be nested arbitrarily into each other, within a procedure or over different procedure calls, as long as any inner trytryTryTrytry-catchcatchCatchCatchcatch-endtryendtryEndtryEndtryendtry block lies completely either within an outer trytryTryTrytry-catchcatchCatchCatchcatch or a catchcatchCatchCatchcatch-endtryendtryEndtryEndtryendtry block. If an exception is thrown within an inner trytryTryTrytry-catchcatchCatchCatchcatch block, the exception handling is caught in the corresponding catchcatchCatchCatchcatch-endtryendtryEndtryEndtryendtry block. Hence, the exception is not visible for the outer trytryTryTrytry-catchcatchCatchCatchcatch blocks unless the exception is rethrown explicitly by calling a throwthrowThrowThrowthrow operator from the catchcatchCatchCatchcatch block.

If within a HALCON operator an error occurs, an exception tuple is created and passed to the catchcatchCatchCatchcatch operator that is responsible for catching the exception. The tuple collects information about the error such as the error code and the error text. After catching an exception, this information can be accessed with the help of the operator dev_get_exception_datadev_get_exception_dataDevGetExceptionDataDevGetExceptionDatadev_get_exception_data. For more information about the passed exception data, how to access them, and considerations about the code export, see the description of that operator. The reference of the operator throwthrowThrowThrowthrow describes how to throw user-defined exception tuples.

HDevelop offers the opportunity to disable the handling of HALCON errors. This can be achieved by calling the operator dev_set_check('~give_error')dev_set_check("~give_error")DevSetCheck("~give_error")DevSetCheck("~give_error")dev_set_check("~give_error") or by unchecking the check box Give Error on the dialog Edit->Preferences->Runtime Settings. If the error handling is switched off, in case of an HALCON error no exception is thrown but the program execution is continued as normal at the next operator. In contrast to that, the operator throwthrowThrowThrowthrow will always throw an exception independently of the 'give_error'"give_error""give_error""give_error""give_error" setting. The same applies if an error occurred during the evaluation of an parameter expression.

Attention

The export of the operators trytryTryTrytry, catchcatchCatchCatchcatch, endtryendtryEndtryEndtryendtry, and throwthrowThrowThrowthrow is not supported for the language C, but only for the languages C++, C# and VisualBasic/.NET. Only the latter support throwing exceptions across procedures.

Example (HDevelop)

try
  read_image (Image, 'may_be_not_available')
catch (Exception)
  if (Exception[0] == 5200)
    dev_get_exception_data (Exception, 'error_message', ErrMsg)
    set_tposition (3600, 24, 12)
    write_string (3600, ErrMsg)
    return ()
  else
    * rethrow the exception
    throw ([Exception,'unknown exception in myproc'])
  endif
endtry

Result

trytryTryTrytry always returns 2 ( H_MSG_TRUE) .

Alternatives

dev_set_checkdev_set_checkDevSetCheckDevSetCheckdev_set_check

See also

catchcatchCatchCatchcatch, endtryendtryEndtryEndtryendtry, throwthrowThrowThrowthrow, dev_get_exception_datadev_get_exception_dataDevGetExceptionDataDevGetExceptionDatadev_get_exception_data, dev_set_checkdev_set_checkDevSetCheckDevSetCheckdev_set_check

Module

Foundation