Exceptions Raised in Java objects

J-Integra® transparently maps between COM exceptions and Java exceptions. COM components can invoke methods in Java objects using J-Integra®.

If such a method generates an exception, J-Integra® will catch the exception, and translate it to an appropriate COM exception.

Take, for example, the following Java code. Note that the 'method1' method is not particularly sound:

import java.io.*;
public class Simple {
  public static void main(String[] args) throws Exception {

     com.linar.jintegra.Jvm.register("firstjvm");
     Thread.sleep(6000000); // Sleep for an hour
  }

  public void method1() throws java.io.IOException {
     throw new java.io.IOException("A deliberate exception");
  }
}
}

This is the Visual Basic client code:

Public Sub method1(ByVal p1 As Object)
  Set p1 = GetObject("firstjvm:Simple")
  On Error GoTo ErrorHandler
  p1.method1
  ErrorHandler: ' Error-handling routine.
     MsgBox Err.Source, vbInformation, "Source"
     MsgBox Err.Description, vbInformation, "Description"
     MsgBox Str(Err.Number), vbInformation, "Code"
End Sub

The Visual BASIC code simply establishes an error handler, and then makes a calls into the Java object. The Java object in our example deliberately generates an exception which is caught by the Visual BASIC error handler.

The error handler displays a series of message boxes, which give information on the error:

The source J-Integra® automatically fills in the source with the stack trace of method which generated the error:

The description J-Integra® automatically fills in the description with the string returned from Exception.getMessage():

We have erased the part of the stack trace showing J-Integra® internal methods.

The code J-Integra® automatically sets the code to 0x80020009, which is COM for 'Exception occurred.':

What if you want to specify the source/description/code yourself?

If you wish explicitly set the error information yourself, rather than accepting J-Integra®'s default, then you can throw an instance of com.linar.jintegra.AutomationException.

If we change the Java code in the above example:

   public void method1() throws com.linar.jintegra.AutomationException {
     long code = 0x80020009;
     String source = "The source of the exception";
     String description = "A demonstration description";
     throw new com.linar.jintegra.AutomationException(code, source, description);
  }

The following message boxes get displayed, with the information that was explicitly set: