Exceptions raised in COM components

J-Integra® transparently maps between COM exceptions and Java exceptions.

When a Java client invokes a method in a COM component, that component may raise an exception.

COM Exceptions have an associated error number, a source, and a description.

Raising exceptions in a COM component

In Visual BASIC, an exception can be raised like this:

Public Sub errorMethod()
  Err.Raise vbObjectError + 1051, "My program", "This is the description"
End Sub

In Visual C++, an exception can be raised like this:

AfxThrowOleDispatchException(0, "I am sorry Dave, I can't do that...");

Catching them in Java

When a COM component throws an exception, an instance of com.linar.jintegra.AutomationException is thrown in the Java client.

The AutomationException class exposes methods to obtain the error number, source and description of the COM exception that was thrown.

import com.linar.jintegra.AuthInfo;
import java.net.UnknownHostException;
import java.io.IOException;
public class Example {
  public static void main(java.lang.String[] args) throws IOException, UnknownHostException {
     vbexcep.Class1 c1 = null;
     try {
        c1 = new vbexcep.Class1();
        c1.errorMethod();
     } catch(com.linar.jintegra.AutomationException ae) {
        System.out.println("Caught: " + ae);
        System.out.println("Source: " + ae.getSource());
        System.out.println("Description: " + ae.getDescription());
        System.out.println("Code: " + ae.getCode());
     } finally {
        com.linar.jintegra.Cleaner.releaseAll();
     }
  }
}

The result of running the example:

Use IOException rather than AutomationException?

If you don't like the idea of having to deal with the AutomationException class, then you can catch java.io.IOException instead, since AutomationException derives from IOException. In this way you can reduce the dependancy of your code on COM related classes, at the expense of not being able to access the error code, description and source.