Please enable JavaScript to view this site.

DAQFactory User's Guide

Navigation: 5 Sequences & Scripting

5.19 Error handling with try/catch

Scroll Prev Top Next More

Unfortunately, unless your sequences are rather simple, errors will probably occur in your sequences.  Something as simple as a device getting unplugged could generate an error.  The error must then either be caught by your sequence code, or the sequence will stop and display an alert.  

The try and catch statements are used to catch errors so that the sequence doesn't stop.  They allow you to decide what happens when there is an error:

 

try

  do something here...

catch()

  do something if an error occurs

endcatch

The try statement marks the beginning of a block of steps that may throw an error.  If an error occurs after the try statement, DAQFactory will jump to the catch statement and execute the steps immediately following it, if any, and then proceed with the steps after the endcatch.  

Catch statements can also be set to catch particular errors.  Since all errors within DAQFactory start with a code, you can specify the code of the error to catch:

 

try

  private x = nonexistantchannel + 5

catch("C1000")

  do something if error code C1001 occurs

endcatch

catch()

  do something if any other error occurs

endcatch

In this case we can do two different things depending on the error that actually occurs.  Only the first catch that matches will execute.  The string specified in the catch statement must simply match the same number of characters as the error itself.  So 'catch("C")' would catch all errors that start with the letter C.

Try/Catch statements can be nested as needed.  They will also carry through to called functions.  If an error occurs in a called function and that function does not handle it, the function will return and the calling sequence will have to handle the error.

You can access the text of the caught error with the private variable strLastError.  For example, you could generate an error message:

 

catch()

   ? strLastError

endcatch

strLastError is a simple string variable.  In addition, the private variable lastErrorAlert is available.  This contains an internal object that you can print using ? that formats the message the same way the system would, displaying the full call stack.  This object can only be used with the ? statement inside the catch().

You can also generate your own errors with the throw statement.  This can be used as a way to branch, or as a way of creating your own codes:

 

try

...

  if (Input[0] > 5)

    throw("U1001 Input went out of bounds")

  endif

  ...

catch("U1001")

endcatch

If the error is not caught it will be treated like any other error and stop the sequence and generate an alert.

The error string inside the throw is optional.  If the throw statement is inside a catch, it will simply rethrow the same error and is the same as throw(strLastError).  If it is outside the catch it will generate a default "User Specified Error".

Note: If you have multiple catch statements with a single try and you throw in the first catch, the other catch statements may catch the error you just generated:

 

try

  ...

catch("C1001")

  throw("my error")

endcatch

catch()

endcatch

In the above example, if error code "C1001" occurs inside the try, the first catch statement will handle it.  This will then execute the throw which will throw another error.  But, there is another catch left that catches all errors and this will then catch our new "my error".

Ignoring errors

 

The ignore() function has been removed from DAQFactory.  Use try/catch instead to handle errors.