Main Content

ErrorOccurred Event

(Not recommended) Notify when device-related errors occur

This session object function is not recommended. Use DataAcquisition object functions instead. See Compatibility Considerations.



lh = addlistener(session,'ErrorOccurred',callbackfct); creates a listener for the ErrorOccurred event. When an error occurs, the callback is executed. The callback can be any MATLAB® function with the (src,event) signature.


In background mode, errors and exceptions are not displayed by default. Use the ErrorOccurred event listener to display the errors.


lh = addlistener(session,'ErrorOccurred',@(src,event) expr); creates a listener for the ErrorOccurred event and fires an anonymous function. The anonymous function requires the specified input arguments and executes the operation specified in the expression expr. Anonymous functions provide a quick means of creating simple functions without requiring that your function be saved in a separate file. For more information, see Anonymous Functions.

The callback has two required parameters: src and event. src is the session object for the listener, and event is a daq.ErrorOccurredInfo object. The daq.ErrorOccurredInfo object contains the Error property, which is the MException associated with the error. You can use the getReport method to return a formatted message that uses the same format as errors thrown by internal MATLAB code.


Add a Listener to Display an Error Report

Create a session, and add an analog input channel.

s = daq.createSession('ni');

Get a formatted report of the error.

lh = addlistener(s,'ErrorOccurred',@(src,event) disp(getReport(event.Error)));

Acquire data, wait, and delete the listener.


Input Arguments

collapse all

Data acquisition session, specified as a session object. Create the session object using daq.createSession. Use the data acquisition session for acquisition and generation operations. Create one session per vendor and use that vendor session to perform all data acquisition operations.

Callback function, specified as a function handle.

Anonymous callback function, specified as a MATLAB operation. The expression executes when the trigger occurs.

Version History

Introduced in R2010b

collapse all

R2020a: session object interface is not recommended

Use of this function with a session object is not recommended. To access a data acquisition device, use a DataAcquisition object with its functions and properties instead.

For more information about using the recommended functionality, see Transition Your Code from Session to DataAcquisition Interface.