Bridges > Java Import Bridge > Java Exceptions

# Java Exceptions

During the operation of the bridge, an error may occur when initializing the bridge, creating an IDLjavaObject, calling methods, setting properties, or getting properties. Typically, these errors will be fixed by changing your IDL or Java code (or by changing the bridge configuration). Java bridge errors operate like other IDL errors in that they stop execution of IDL and post an error message. These errors can be caught like any other IDL error.

On the other hand, Java uses the exception mechanism to report errors. For example, in Java, if we attempt to create a java.lang.StringBuffer of negative length, a java.lang.NegativeArraySizeException is issued.

Java exceptions are handled much like bridge errors. They stop IDL execution (if uncaught) and they report an error message containing a line number. In addition, a mechanism is provided to grab the exception object (a subclass of java.lang.Throwable) via the session object. Once connected with the exception object, IDL can call any of the methods provided by this Java object. For example, IDL can query the exception name to determine how to handle it, or print a stack trace of where the exception occurred in your Java code.

The exception object is provided through the GetException method to the IDLJavaBridgeSession object. See The IDLJavaBridgeSession Object for more information about this object.

## Uncaught Exceptions

If a Java exception is not caught, IDL will stop execution and display an Exception thrown error message. For example, when the following program is saved as ExceptIssued.pro, compiled, and ran in IDL:

PRO ExceptIssued

; This will throw a Java exception

oJStrBuffer = OBJ_NEW($

'IDLJavaObject$java_lang_StringBuffer', $

’java.lang.StringBuffer’, -2)

END

IDL issues the following output:

IDL> ExceptIssued

% Exception thrown

% Execution halted at: EXCEPTISSUED 4 ExceptIssues.pro

% $MAIN$

From the IDL command line, you can then use the session object to help debug the problem:

IDL> oJSession = OBJ_NEW('IDLJavaObject$IDLJAVABRIDGESESSION')

IDL> oJExc = oJSession->GetException()

IDL> oJExc->PrintStackTrace

% java.lang.NegativeArraySizeException:

% at java.lang.StringBuffer.<init>(StringBuffer.java:116)

Note: A similar example is also provided in the exception.pro file, which is in the resource/bridges/import/java/examples directory of the IDL distribution. The exception.pro example shows how to use the utility routine provided in the showexcept.pro file. This showexcept utility routine can be re- used to provide consist error messages when Java exceptions occur. The showexcept.pro file is also provided in the resource/bridges/import/java/examples directory of the IDL distribution. Run the example procedure by entering exception at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT exception.pro.

## Caught Exceptions

Java exceptions can be caught just like IDL errors. Consult the documentation of the Java classes that you are using to ensure IDL is catching any expected exceptions. For example:

PRO ExceptCaught

; Grab the special IDLJavaBridgeSession object

oJBridgeSession = OBJ_NEW('IDLJavaObject$IDLJAVABRIDGESESSION')

bufferSize = -2

; Our Java constructor might throw an exception, so let’s catch it

CATCH, error_status

IF (error_status NE 0) THEN BEGIN

; Use session object to get our Exception

oJExc = oJBridgeSession->GetException()

; should be of type

; IDLJAVAOBJECT$JAVA_LANG_NEGATIVEARRAYSIZEEXCEPTION

HELP, oJExc

; Now we can access the members java.lang.Throwable

PRINT, 'Exception thrown:', oJExc->ToString()

oJExc->PrintStackTrace

; Cleanup

OBJ_DESTROY, oJExc

; Increase the buffer size to avoid the exception.

bufferSize = bufferSize + 100

ENDIF

; This throws a Java exception the 1st time, but pass the 2nd time.

oJStrBuffer = OBJ_NEW('IDLJavaObject$java_lang_StringBuffer', $

'java.lang.StringBuffer', bufferSize)

OBJ_DESTROY, oJStrBuffer

OBJ_DESTROY, oJBridgeSession

END

Note: A similar example is also provided in the exception.pro file, which is in the resource/bridges/import/java/examples directory of the IDL distribution. The exception.pro example shows how to use the utility routine provided in the showexcept.pro file. This showexcept utility routine can be re- used to provide consist error messages when Java exceptions occur. The showexcept.pro file is also provided in the resource/bridges/import/java/examples directory of the IDL distribution. Run the example procedure by entering showexcept at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT showexcept.pro.