LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Basic error handling: how to catch all errors in a portion of code (and act accordingly)

I have a portion of code in a VI that I know is likely to fail at some point (opening a file, then reading two arrays from it: the file can be absent, or contain the wrong data). I would like to be able to catch any errors that may occur during this procedure, and if such an error occurs launch a pre-defined action (return a default value, for instance). Is there a simple way to do this, in a way similar to the disabling of a portion of code with a Diagram Disable structure ?
0 Kudos
Message 1 of 7
(2,633 Views)

Yes.  Wire all the error clusters. When you want to take an action on an error, wire the error cluster wire to the selector inpput of a case structure. Inside the Error case, take what action you need.

 

 

Error handling.png

 

 

Lynn

0 Kudos
Message 2 of 7
(2,627 Views)
Thank you very much for this suggestion. But how can I produce/modify an error cluster when a given piece of code fails ? (in a blank VI, no error cluster is visible ; to use the method you have suggested, I would need to produce such an error cluster from a piece of code in the VI... am I correct ?)
0 Kudos
Message 3 of 7
(2,624 Views)
For instance, consider that I'm trying to access (through "Index Array") an array element that might not exist..
0 Kudos
Message 4 of 7
(2,621 Views)

use the "bundle by name" function to set the individual elements of the error cluster to the values you want.

0 Kudos
Message 5 of 7
(2,620 Views)

You shuold probably start with the on-line tutorials for Getting Started with LV.  Those may help with some of these basic questions.

 

If you drop an error in cluster onto the front panel, then you get a terminal as in my image on the block diagram.  The default value of the error cluster is no error.  As Greg said, use Bundle by Name to insert the values you want.  You can clear errors (but not eliminate the cause!) by wiring default values to the bundle node.

 

Look at the LV help for lots of information on errors and how to handle them.

 

Lynn

0 Kudos
Message 6 of 7
(2,604 Views)
Thank you for your help ! 🙂
0 Kudos
Message 7 of 7
(2,602 Views)