powered by Jive Software

GroupChat.leave() should throw or catch an exception

The leave() method in GroupChat does not require (or allow wrapping in a try { groupChat.leave(); } catch (XMPPException e) { do something } wrapper.

However, it occasionally happens that leave()-ing a group chat, when the client is no longer connected to the server, will definitely cause an exception, which percolates up and causes a problem.

So, would it be possible to either (1) catch and handle the exception generated when leave() is called and the client is in an improper state, or (2) make the user handle the XMPPException himself?

I’‘m sorry that I don’'t have the stacktrace here, as this happened at home, and I am at the office now. If I see it again, I will post it here. But it really does happen. Honest! ^^

Here is the exception:

Exception in thread “main” java.lang.IllegalStateException: Not connected to server.

at org.jivesoftware.smack.XMPPConnection.sendPacket(XMPPConnection.java:571)

at org.jivesoftware.smack.GroupChat.leave(GroupChat.java:193)

This can happen if a leave() is attempted after a connection loss, but without any intervening traffic before the leave() call.

So, if I catch the IllegalStateException, then everything is OK. However, doesn’'t this need to be either caught or thrown to avoid this little pitfall?