ClassesClasses | | Operators

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.

trytryTryTry (Operator)

Name

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

Signature

try( : : : )

Herror try()

Herror T_try()

void Try()

static void HOperatorSet.Try()

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 messages (throw directly an HDevelop Exception)'"Suppress error messages (throw directly an HDevelop Exception)""Suppress error messages (throw directly an HDevelop Exception)""Suppress error messages (throw directly an HDevelop Exception)""Suppress error messages (throw directly an HDevelop Exception)" that can be reached via 'Edit->Preferences->General Options->Experienced User'"Edit->Preferences->General Options->Experienced User""Edit->Preferences->General Options->Experienced User""Edit->Preferences->General Options->Experienced User""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_dataDevGetExceptionDataDevGetExceptionDataDevGetExceptionData. 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")DevSetCheck("~give_error") or by unchecking the check box 'Give Error'"Give Error""Give Error""Give Error""Give Error" on the dialog 'Edit->Preferences->Runtime Settings'"Edit->Preferences->Runtime Settings""Edit->Preferences->Runtime Settings""Edit->Preferences->Runtime Settings""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_checkDevSetCheckDevSetCheckDevSetCheck

See also

catchcatchCatchCatchCatch, endtryendtryEndtryEndtryEndtry, throwthrowThrowThrowThrow, dev_get_exception_datadev_get_exception_dataDevGetExceptionDataDevGetExceptionDataDevGetExceptionData, dev_set_checkdev_set_checkDevSetCheckDevSetCheckDevSetCheck

Module

Foundation


ClassesClasses | | Operators