Error handling with LS2J
While using LS2J, LotusScript® returns error messages when things go wrong. However, these messages may be misunderstood without additional accurate information.
The error model
The Java™ error model is catch and throw. The LotusScript® error model is the ON ERROR statement and the error handling block. LotusScript® catches the Java™ error and maps it to its error processing model. This allows the LotusScript® user to manipulate the Java™ error with LotusScript® error semantics through the use of the JavaError class.
The user should look at the LotusScript® Error before the JavaError properties. If an error is trapped within LotusScript® before Java™ is called, JavaError.ErrorMsg and JavaError.StackTrace are empty strings. Note the following code:
Uselsx *javacon
Sub Initialize
Dim jSession As JavaSession
Dim cls As JavaClass
Dim obj As JavaObject
Dim msg As String
Dim jError As JavaError
On Error Goto ErrorHandling
Set jSession = New JavaSession()
Set cls = jSession.GetClass("java/lang/Short")
' This signature would not match any Constructor
Set obj = cls.CreateObject("(X)V", 1)
Print obj.toString()
Exit Sub
ErrorHandling:
Print Error ' "LS2J Error: Constructor failed to execute"
Set jError = jSession.getLastJavaError
Print "Java error: " jError.ErrorMsg ' empty String
Exit Sub
End Sub
The user attempts to call the java.lang.Short Constructor. The correct call is the signature for a short parameter:
Set obj = cls.CreateObject("(S)V", 1)
Instead the user calls the following:
Set obj = cls.CreateObject("(X)V", 1)
Since "X" doesn't match any Java™ type, LotusScript® raises an error before calling Java™. The only error message is in the LotusScript® error:
LS2J Error: Constructor failed to execute
If the LotusScript® portion of LS2J cannot detect an error, it calls Java™. Suppose the code reads:
Set obj = cls.CreateObject("(I)V", 1)
This signature could match a Constructor with an int parameter. LotusScript® detects no error; attempts to call the Java™ Constructor, and fails because the java.lang.Short class has no Constructor with an int parameter. The LotusScript® Error is still:
LS2J Error: Constructor failed to execute
But because Java™ was called, there is also a jError.ErrorMsg:
java.lang.NoSuchMethodError: <init>
The JavaError object, when retrieved from the JavaSession, contains the last error and the last StackTrace.
Example code using JavaError
Sub Initialize
Dim mySession As New JavaSession
Dim myError As JavaError
On Error GoTo ErrorHandling
'...
' code here
'....
Exit Sub
ErrorHandling:
Set myError = mySession.getLastJavaError
print Error
print myError.ErrorMsg
print myError.StackTrace
End Sub
This code sample prints the LotusScript® error, the Java™ error, and the Java™ StackTrace.
Examples of LotusScript® errors that might be misunderstood
Example 1:
You try to instantiate an object, but you have the wrong signature or number of arguments.
LotusScript® says: |
LS2J: Null JavaObject |
---|---|
Java™ ErrorMsg says: |
java.lang.NoSuchMethodError: <init> |
Example 2:
You try to instantiate an object, but an error occurs in the Constructor.
LotusScript® says: |
LS2J: Null JavaObject |
---|---|
Java™ StackTrace says: |
java.lang.ArrayIndexOutOfBoundsException at myGraph.<init>(Compiled Code) |
Example 3a:
You try to execute a method, but use the wrong number of arguments.
LotusScript® says: |
LS2J: Parameter mismatch calling Method <Method Name here> |
---|---|
Java™ ErrorMsg says: |
LS2J error |
Example 3b:
Now, you execute the method with the correct arguments but there is an error in the method.
LotusScript® says: |
LS2J: Parameter mismatch calling Method <Method Name here> |
---|---|
Java™ StackTrace says: |
java.lang.ArrayIndexOutOfBoundsException: 3 at myGraph.setOrientation(myGraph.java:262) |