try — Starts a program block where exceptions are detected and caught.
try( : : : )
With the help of the operators
throw 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
two code blocks are formed: the first one (
contains the watched program lines that perform the normal program logic.
The second block (
endtry) contains the code
that has is executed if an exception occurs.
try enables the exception handling for the following
program lines, i.e., the following code block up to the corresponding
catch 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
try block is left immediately
(or---depending on a user preference---after displaying an error message box)
and the program execution continues at the corresponding
If the exception is thrown within a procedure that was called from the
try block (directly or via other procedure calls), the procedure
call and all intermediate procedure calls that are on the call stack above
try 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
The program block that is watched for exceptions ends with the corresponding
If within the watched
try block no exception occurred, the following
catch block is ignored and the program execution continues after
endtry blocks can be nested arbitrarily
into each other, within a procedure or over different procedure calls,
as long as any inner
endtry block lies
completely either within an outer
If an exception is thrown within an inner
the exception handling is caught in the corresponding
endtry block. Hence, the exception is not
visible for the outer
catch blocks unless the
exception is rethrown explicitly by calling a
If within a HALCON operator an error occurs, an exception tuple is created
and passed to the
catch operator that is responsible for catching
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
For more information about the passed exception data, how to access them,
and considerations about the code export, see the description of that
The reference of the operator
throw 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') or by unchecking the check box
'Give Error' on the dialog
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
In contrast to that, the operator
throw will always throw an
exception independently of the 'give_error' setting.
The same applies if an error occurred during the evaluation of an
The export of the operators
throw 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.
try read_image (Image, 'may_be_not_available') catch (Exception) if (Exception == 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
try always returns TRUE.