Skip to main content

Safe API — Never Throw Exception Variants

Overview​

The UI main thread / batch jobs must not be interrupted by an OpcUaException thrown on an RPC failure, so the SDK provides a Safe variant for each common call — every exception is folded into an Enums.StatusCode, which the caller reads from the status field of the return value. The return types are BrowseResult / ReadResult / CallResult / HistoryResult.

Corresponding sections of the standard: Browse Part 4 §5.8, Read §5.10.2, Write §5.10.4, Call§5.11.2, HistoryRead Part 11§6.5.

API​

MethodsCategoryReading and WritingNote
ua.browseSafe(nodeId, filter)BrowseReadfilter is an Enums.NodeClass; returns BrowseResult { status, references }
ua.browseSafe(nodeId)BrowseReadSame as above; set "filter" to "Unspecified"
ua.readSafe(nodeId, attribute)ReadReadThe attribute is an Enums.AttributeId; returns a ReadResult { status, value }; if the operation fails, value is null
ua.readSafe(nodeId)ReadReadSame as above; the attribute takes the value
ua.writeSafe(nodeId, value, attribute)WriteWritevalue is OpcUaVariant; returns Enums.StatusCode
ua.writeSafe(nodeId, value)WriteWriteSame as above; the attribute takes the value
ua.callSafe(objectId, methodId, varargs)MethodsWriteInput parameters: OpcUaVariant...; Returns CallResult { status, outputs };
ua.readHistorySafe(nodeId, start, end, maxValues)HistoryReadReturns HistoryResult { status, values }

Code Example​

import com.darra.opcua.*;

try (OpcUaSession ua = new OpcUaSession("opc.tcp://localhost:4840")) {
ua.connect();

// 1) Browsing—A single point of failure should not cause the entire tree to report an error
BrowseResult br = ua.browseSafe("ns=2;s=Boiler1", Enums.NodeClass.Unspecified);
if (!br.isGood()) {
System.out.println("Browse 失败 " + br.status());
} else {
for (Reference r : br.references())
System.out.println(r.getBrowseName());
}

// 2) Batch Polling - A single failure does not interrupt the process
for (String nid : nodeList) {
ReadResult rr = ua.readSafe(nid, Enums.AttributeId.Value);
if (rr.isGood()) {
try (OpcUaDataValue dv = rr.value()) {
System.out.println(nid + " = " + dv.variant());
}
} else {
System.out.println(nid + " = <" + rr.status() + ">");
}
}

// 3) Batch deployment of parameters
for (Map.Entry<String, Double> sp : setpoints.entrySet()) {
try (OpcUaVariant v = new OpcUaVariant().setDouble(sp.getValue())) {
Enums.StatusCode rc = ua.writeSafe(sp.getKey(), v);
if (rc != Enums.StatusCode.Good)
System.out.println("写 " + sp.getKey() + " 失败 " + rc);
}
}

// 4) Calling the Method
CallResult cr = ua.callSafe("ns=2;s=Robot", "ns=2;s=Robot.Home");
if (cr.status() != Enums.StatusCode.Good)
System.out.println("Home 失败: " + cr.status());

// 5) History Reading
HistoryResult hr = ua.readHistorySafe("ns=2;s=Temp", t0, t1, 5000);
try {
if (hr.isGood()) {
for (OpcUaDataValue dv : hr.values())
System.out.println(dv.variant());
}
} finally {
for (OpcUaDataValue dv : hr.values()) if (dv != null) dv.close();
}
}

Relationship to the Retry Mechanism​

Normal read / write / browse / method call / history read encountered an errorSell immediately OpcUaException, no reconnection or retry. The Safe variant simply wraps the exception into the return value ( status OpcUaException returning true Throwable if successful ex.statusCode, Throwable otherwise Enums.StatusCode.Bad), and likewise does not reconnect or retry. Session-level automatic reconnection is handled in the background by the underlying stack and is independent of the RPC success or failure of a single request—see Automatic Reconnection。

Best Practices​

  • On the UI main thread (Swing/JavaFX) always use Safe — an exception escaping into the EDT will hang the UI.
  • Safe for Batch Processing / Inspection Cycles
  • The business critical path still uses the standard version

Cross-Language Comparison​

C#PythonJavaC++RustC
BrowseSafebrowse_safebrowseSafeBrowseSafebrowse_safeDarraUa_Session_BrowseSafe
ReadSaferead_safereadSafeReadSaferead_safeDarraUa_Session_ReadSafe
WriteSafewrite_safewriteSafeWriteSafewrite_safeDarraUa_Session_WriteSafe
CallSafecall_safecallSafeCallSafecall_safeDarraUa_Session_CallSafe
ReadHistorySaferead_history_safereadHistorySafeReadHistorySaferead_history_safeDarraUa_Session_ReadHistorySafe

Next Step​