User`s guide

68 Agilent VISA User’s Guide
3 Programming with VISA
Trapping Errors
This section provides guidelines for trapping errors, including:
Trapping Errors
Exception Events
Trapping Errors
The example programs in this guide show specific VISA functionality
and do not include error trapping. Error trapping, however, is good
programming practice and is recommended in all your VISA application
programs. To trap VISA errors you must check for VI_SUCCESS after
each VISA function call.
If you want to ignore WARNINGS, you can test to see if err is less than
(<) VI_SUCCESS. Since WARNINGS are greater than VI_SUCCESS
and ERRORS are less than VI_SUCCESS, err_handler would only be
called when the function returns an ERROR. For example:
if(err < VI_SUCCESS) err_handler (vi, err);
Example: Checking for VI_SUCCESS This example illustrates
checking for VI_SUCCESS. If VI_SUCCESS is not returned, an error
handler (written by the programmer) is called. This must be done with
each VISA function call.
ViStatus err;
.
.
err=viPrintf(vi, "*RST\n");
if (err < VI_SUCCESS) err_handler(vi, err);
.
.
Example: Printing Error Code The following error handler prints
a user-readable string describing the error code passed to the function:
void err_handler(ViSession vi, ViStatus err){