【发布时间】:2013-02-20 14:51:40
【问题描述】:
我have found 是获取LocateRegistry 的安全方法(即使注册表尚不存在)。
Registry registry = null;
try {
registry = LocateRegistry.getRegistry(52365);
registry.list();
// This call will throw an exception if the registry does not already exist
}
catch (RemoteException e) {
registry = LocateRegistry.createRegistry(52365);
}
是否可以先检查注册表是否存在,根据检查结果使用getRegistry或createRegistry?
【问题讨论】:
-
没有。如果函数中有错误,您需要捕获它并做出相应的反应。
-
@mercutio 我不会尝试完全避免异常。但我想避免在不存在时不必要地调用
getRegistry。我已经更新了问题。