我有一个类,我在这个类中有一个方法。
方法:
public void login(String username,String password) throws XMPPException, SmackException, IOException {
ConnectionConfiguration configuration=new ConnectionConfiguration("", 5222,"localhost");
configuration.setSecurityMode(SecurityMode.disabled);
connection=new XMPPTCPConnection(configuration);
try {
connection.connect();
connection.login(username,password);
ChatManager chatmanager = ChatManager.getInstanceFor(connection);
chatmanager.addChatListener(new ChatManagerListener() {
public void chatCreated(final Chat chat, final boolean createdLocally) {
chat.addMessageListener(messageListener);
}
});
}
catch (XMPPException | SmackException | IOException e) {
e.printStackTrace();
}
}
Xmpp 需要 try/catch block 。我在方法中定义了 try/catch。但是当我尝试在主类中使用此方法时,出现编译器错误:
Unhandled exception type IOException
我是这样使用的:
SmackClass smack=new SmackClass();
smack.login("asdad","asdasd");
我该如何解决这个问题?
最佳答案
XMPPException、SmackException、IOException
从 login
方法的 throws 子句中删除这些。
它们会导致主类中出现编译时错误。
一般来说,不要声明抛出那些异常
您已经在方法本身中处理了。
关于java - Try/catch block 内部方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27662697/