Exceptions
When an error occurs, the IMu Java API throws an exception. The exception is
an IMuException
object. This is a subclass of Java's standard
Exception
class.
For simple error handling all that is usually required is to catch the exception and report the exception as a string:
try { // ⋯ }catch (IMuException e) { System.out.println("Error: " + e); System.exit(0); }
The IMuException
class overrides the Exception
class's toString()
method and returns an error message.
To handle specific IMu errors it is necessary to check the exception is an
IMuException
object before using it. The IMuException
class
includes a property called id
. This is a string and contains the internal
IMu error code for the exception. For example, you may want to catch the
exception raised when a Session
object's connect
method fails and
try to connect to an alternative server:
String mainServer = "server1.com"; String alternativeServer = "server2.com"; Session session = new Session(); session.setHost(mainServer);try { session.connect(); }catch (IMuException e) { // Check for specific SessionConnect errorif (e.getID() != "SessionConnect") { System.out.println("Error: " + e); System.exit(1); } session.setHost(alternativeServer); try { session.connect(); } catch (Exception e) { System.out.println("Error: " + e); System.exit(1); } } /* * By the time we get to here the session is connected to either the main * server or the alternative. */