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
| Methods | Category | Reading and Writing | Note |
|---|---|---|---|
| ua.browseSafe(nodeId, filter) | Browse | Read | filter is an Enums.NodeClass; returns BrowseResult { status, references } |
| ua.browseSafe(nodeId) | Browse | Read | Same as above; set "filter" to "Unspecified" |
| ua.readSafe(nodeId, attribute) | Read | Read | The attribute is an Enums.AttributeId; returns a ReadResult { status, value }; if the operation fails, value is null |
| ua.readSafe(nodeId) | Read | Read | Same as above; the attribute takes the value |
| ua.writeSafe(nodeId, value, attribute) | Write | Write | value is OpcUaVariant; returns Enums.StatusCode |
| ua.writeSafe(nodeId, value) | Write | Write | Same as above; the attribute takes the value |
| ua.callSafe(objectId, methodId, varargs) | Methods | Write | Input parameters: OpcUaVariant...; Returns CallResult { status, outputs }; |
| ua.readHistorySafe(nodeId, start, end, maxValues) | History | Read | Returns 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# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| BrowseSafe | browse_safe | browseSafe | BrowseSafe | browse_safe | DarraUa_Session_BrowseSafe |
| ReadSafe | read_safe | readSafe | ReadSafe | read_safe | DarraUa_Session_ReadSafe |
| WriteSafe | write_safe | writeSafe | WriteSafe | write_safe | DarraUa_Session_WriteSafe |
| CallSafe | call_safe | callSafe | CallSafe | call_safe | DarraUa_Session_CallSafe |
| ReadHistorySafe | read_history_safe | readHistorySafe | ReadHistorySafe | read_history_safe | DarraUa_Session_ReadHistorySafe |