我有本机lib crated从unix socket读取。当我无法连接到unix socket时,我想以某种方式在java中捕获它。
在cpp我有:
if (send(fd, input, strlen(input)+1, 0) == -1) {
//perror("send");
retText = err("Cannot send to unix socket");
ok = 0;
}
return retText;
现在在java中我只希望得到带有错误信息的retText,而是在错误时jvm关闭并打印消息:
java ::连接被拒绝
在java代码中我有:
try {
System.out.println("NATIVE EXECUTING");
byte[] bytes = exec(socketFile, channelId, jsonCommand);
System.out.println("NATIVE EXECUTED");
bytes[bytes.length - 1] = (byte) ' ';
String ret = new String(bytes, StandardCharsets.UTF_8);
if (ret.startsWith("ERROR: ")) {
throw new StoredException("Cannot execute rpc command: " + ret.substring(7));
}
return ret;
} catch (Throwable ex) {
System.out.println("EXCEPTION THROWN");
throw new StoredException("Cannot execute rpc command", ex);
}
程序输出的最后两行:
NATIVE EXECUTING
java: : Connection refused
root@tst-cln-01:~/ln#