![]() |
![]() |
|
|
| |
Exceptions
WebLogic jCOM transparently maps between COM exceptions and Java exceptions. The following sections look at:
Exceptions raised in COM components
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.bea.jcom.AutomationException is thrown in the Java client. The javadoc documentation associated with that class is here.
The AutomationException class exposes methods to obtain the error number, source and description of the COM exception that was thrown.
import com.bea.jcom.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.bea.jcom.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.bea.jcom.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.
Exceptions raised in Java objects
COM components can invoke methods in Java objects using WebLogic jCOM.
If such a method generates an exception, then WebLogic jCOM will catch the exception, and translate it into 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.bea.jcom.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 call 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
WebLogic jCOM automatically fills in the source with the stack trace of method which generated the error:
The description
WebLogic jCOM automatically fills in the description with the string returned from Exception.getMessage():
We have erased the part of the stack trace showing WebLogic jCOM internal methods.
The code
WebLogic jCOM 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 to explicitly set the error information yourself, rather than accepting WebLogic jCOM's default, then you can throw an instance of com.bea.jcom.AutomationException.
If we change the Java code in the above example:
public void method1() throws com.bea.jcom.AutomationException { long code = 0x80020009; String source = "The source of the exception"; String description = "A demonstration description"; throw new com.bea.jcom.AutomationException(code, source, description); }
The following message boxes get displayed, with the information that was explicitly set:
Using the Exception Interception Mechanism
We also provide a hook which allows you to provide code which is called when an exception is about to be returned to a COM client. This lets you change the exception information, or store the exception object for later retrieval by a COM client.
To intercept exceptions generated when calling from COM-to-Java, register an interceptor once, like this:
com.bea.jcom.ExceptionInterceptor interceptor = new YourInterceptor(); com.bea.jcom.AutomationException.setExceptionInterceptor (interceptor);
Create a class which implements the ExceptionInterceptor interface like this:
class YourInterceptor implements com.bea.jcom.ExceptionInterceptor {
public com.bea.jcom.AutomationException handleException(Throwable t) {
...
}
}
If you want the default error to be returned to the COM client then simply return null in the handler, otherwise you can generate a specific error, for example:
public com.bea.jcom.AutomationException handleException(Throwable t) {
System.out.println("Intercepting: " + t); long code = 0x80020009; String source = "The source of the exception"; String description = "A demonstration description"; return new com.bea.jcom.AutomationException(code, source, description); }
![]() |
![]() |
![]() |
|
Copyright © 2001 BEA Systems, Inc. All rights reserved.
|