10-14-2010 10:26 PM
I'm learning how to invoke .NET methods in LabVIEW and vice versa. One of my problems is interoperation between errors in LabVIEW and exceptions in .NET.
1. When I try to invoke .NET methods from VIs, if some exceptions are thrown from the .NET methods, is it possible for the VIs to catch the exceptions, and handle them? Right now, I can only get a System.Reflection.TargetInvocationException, and VI execution stops. I'm wondering whether there are some elegant approaches to handle .NET exceptions.
2. When I build VIs into a InterOp assembly, and try to invoke VI functions from C# code. If there are some errors in VIs, how can I pass the errors into C# methods?
Any suggestions and comments will be sincerely appreciated. Thanks.
10-15-2010 05:54 PM
1. Unhandled exceptions from a .NET method call should be caught by the invoke node itself and packaged in the Error Out terminal under error code 1172. Is this not what you are seeing? Do you have Automatic Error Handling enabled for your VI? If so, and the error terminal is not wired for the Invoke Node calling your .NET method, then this could show a dialog prompting the user to continue or abort. Maybe this is what you are seeing?
2. LabVIEW VIs built into an assembly cannot throw exceptions. LabVIEW uses an error code convention, more similar to older C-style applications. I would recommend having your VI being called set an output that is the error code. Then consider building a .NET function wrapper around your VI that checks the error code. If it is not zero, then your function can throw an exception.
10-18-2010 10:39 AM
Thanks for your information. I'll tried according to your message, and get the behavior same you described.
10-20-2016 06:44 PM
It seems like #2 behavior changed after Labview 2012:
http://zone.ni.com/reference/en-XX/help/371361N-01/lvdialog/advanced_net_page/
"When LabVIEW generates a .NET method for a VI, LabVIEW does not export the error in and error out clusters as parameters of the new method. Instead, the new method throws a .NET exception if an error occurs while the method is executing. This exception contains the same information as the error cluster."
I am just looking into this myself and actually just posted a thread here:
http://forums.ni.com/t5/LabVIEW/Catch-Exception-in-c-with-Net-Assembly-Built-in-Labview/m-p/3360501
It seems like LV 2012 and later should throw .net Exception when error cluster out is true but it isn't working for me. I suppose I could do what you suggest by returning the error code, status, and msg myself as outputs or by building my own cluster but I'm interested in your thoughts regarding the documentation. Maybe it only throws an exception for internal labview errors and not based on error cluster out?
10-20-2016 07:27 PM
I apologize. While the exception is thrown, my code was not catching it becuase it was using the old assembly file (before I added my custom error cluster to the error output indicator). The documentation is correct and the exception is thrown in c#.