Unhandled exception type XMPPException

Hi,

I am a newbie to Java, but not programming concepts in general. I am trying to use smack to make a connection to my Openfire server, but my code gives the error “Unhandled exception type XMPPException” when I try to compile.

package org.test;

import org.jivesoftware.smack.*;

public class Login {

public static void main(String[] args) {

XMPPConnection connection = new XMPPConnection(“mfjabber.co.za”);

connection.connect();

connection.login(“test”, “test”);

}

}

The error refers to lines 8 and 9.

I am fairly certain I’m doing something stupid. Any help would be appreciated.

Thanks

Hi xistenz,

You’re not doing anything stupid, you just need to listen to what the compiler is telling you. Exceptions are Java’s way of handling an “event that occurs during the execution of a program that disrupts the normal flow of instructions.” (Check out the exceptions tutorial for more info). In your programs case, when you attempt to connect and login an XMPPConnection can be thrown so you need to handle that. So, this is what you need to do:

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException; public class Login {
   public static void main(String[] args) {
         XMPPConnection connection = new XMPPConnection("mfjabber.co.za");
      try {
         connection.connect();
         connection.login("test", "test");
      } catch (XMPPException e) {
         e.printStackTrace();
         System.out.println(e.getXMPPError());
      }
   }
}

You can see how I wrapped the connect and login methods in a try/catch statement. Typically in the catch block you’ll want to do more than simply print out the stack trace but this will get you going again. I also added a line to print out XMPPError which can be used to help figure out what went wrong when an exception does occur.

Hope this helps,

Ryan