9.3.3 How to reference exceptions on OTM clients
OTM clients cannot directly catch exception classes thrown by EJB applications. Therefore, exception classes are converted on a TSC user proxy (stub) and OTM clients catch the conversion results.
The following describes exceptions that have been converted on a TSC user proxy.
- Organization of this subsection
(1) List of exceptions received by OTM clients
The following table shows the correspondence between exceptions that are generated by EJB applications and exceptions that are received by OTM clients.
|
No. |
Exception generated by EJB applications |
Exception received by OTM clients |
|---|---|---|
|
1 |
Java user-defined exception (one inheriting java.lang.Exception) |
User-defined exception (referencing exception in the IDL definition)# |
|
2 |
Java user-defined exception (one inheriting java.rmi.RemoteException) |
TSC::java::rmi::RemoteEx exception |
|
3 |
Java system-defined exception (one inheriting java.lang.Exception) |
TSCUnknown exception (exception defined in the relevant Java system) |
|
4 |
Java runtime exception (one inheriting java.lang.RuntimeException) |
TSC::java::rmi::RemoteEx exception |
|
5 |
Java system-defined error (one inheriting java.lang.Error) |
TSC::java::rmi::RemoteEx exception |
|
6 |
java.rmi.RemoteException |
TSC::java::rmi::RemoteEx exception |
|
7 |
CORBA system exception (one inheriting org.omg.CORBA.SystemException) |
TSCUnknown exception (exception defined in the relevant TSC system) |
|
8 |
CORBA user exception (one inheriting org.omg.CORBA.UserException) |
User-defined exception (referencing exception in the IDL definition) |
- #
-
Exceptions with names in the xx...xxException format correspond to exceptions with names in the xx...xxEx format. For exceptions whose names do not end with Exception, the corresponding exceptions have the same names with a suffix of Ex. The value of the value member of each exception is an exception structure.
(2) About exception structures
(a) Overview of exception structures
Restrictions placed on an OTM client include inabilities to interpret data that has a recursive structure and to use valuetype. Therefore, an exception thrown by an EJB application is converted into an exception that has a structure as a member as shown in the following figure. This structure is called an exception structure.
|
|
An exception is converted into an exception structure, and then the OTM client catches an exception that has the exception structure as a member. When a user throws a MsgServerException exception from an EJB application, the OTM client catches the exception as a MsgServerEx exception. The value member of the MsgServerEx exception is the MsgServerException exception structure.
|
|
(b) Member of an exception structure
The following table describes the members contained in an exception structure.
|
No. |
Structure member |
Contents |
|---|---|---|
|
1 |
order |
OTM application developers do not need to use these items. These items are required for the OTM client to receive the structure as an exception. |
|
2 |
count |
|
|
3 |
cause |
OTM application developers do not need to use these items. On the OTM client, do not throw an exception from the EJB application by setting any value in cause. |
|
4 |
detailMessage |
Detail message specified in the super class |
|
5 |
stackTrace |
Stack trace up to the time when an exception is generated on the EJB application side |
|
6 |
Member variables |
Member variables set in a Java user-defined exception. For RemoteException, no member variables exist. |
(c) Information items of a stack trace
The stackTrace member of an exception structure is an array for the StackTraceElement structure. The StackTraceElement structure contains a single stack of stack traces.
The following table shows the information set in each member of the StackTraceElement structure.
|
No. |
Field name |
Contents |
Example |
|---|---|---|---|
|
1 |
LineNumber |
Source line number |
29 |
|
2 |
declaringClass |
Class name |
jp.co.Hitachi.soft.test.ejb.testMsgSyncServiceDelEJB |
|
3 |
FileName |
File name |
testMsgSyncServiceDelEJB.java |
|
4 |
MethodName |
Method name |
InvokeBinary |
(3) Example of referencing exception information on an OTM client
The following describes how an OTM client can reference exceptions generated by an EJB application, by using sample code as examples.
In the examples, the following Java user-defined exception is used.
Package jp.co.Hitachi.soft.test;
public class MsgServerException extends Exception
{
public MsgServerException()
{
m_case=1;
}
private int m_case;
}(a) Example of referencing exception information with C++
The following shows an example of referencing exception information with C++ code:
#define ERR_FORMAT "EC=%d,DC=%d,PC=%d,CS=%d,MC1=%d,MC2=%d,MC3=%d,MC4=%d\n"
// Function that returns the length of the specified string
int my_wstringlen(CORBA::WChar* arg){
int i;
for(i=0;arg[i] != 0;i++);
return i;
}
// Function that displays the specified string
void my_print_wstring(CORBA::WChar* arg){
for(int i=0;i < my_wstringlen(arg); i++){
printf("%c", arg[i]);
}
}
int main(int argc, char** argv){
try {
// Processing to invoke the EJB application (omitted)
}catch(jp::co::Hitachi::soft::test::MsgServerEx &e){
// EJB application throws a Java user-defined exception (MsgServerException)
jp::co::Hitachi::soft::test::CSCMsgServerException& ex_val = e.value;
// Output a detail message
printf("detailMessage:");
::TSC::TSCWStringValue& detailMessage = ex_val.detailMessage;
CORBA::WChar* w_detail_msg = new CORBA::WChar[detailMessage.value.length()];
w_detail_msg = TSCgetWString(detailMessage.value );
my_print_wstring(w_detail_msg);
printf("\n");
// Output the m_case member variable
printf("m_case:%d", ex_val.m_case);
// Output a stack trace
::TSC::java::lang::seq1_StackTraceElement& stackTrace = ex_val.stackTrace;
CORBA::ULong len = stackTrace.value.length();
:TSC::java::lang::StackTraceElement stackTraceElement;
for ( int i = 0 ; i < len ; i ++ ) {
stackTraceElement = stackTrace.value [ i ] ;
::TSC::TSCWStringValue& className = stackTraceElement.declaringClass;
::TSC::TSCWStringValue& methodName= stackTraceElement.methodName;
::TSC::TSCWStringValue& fileName = stackTraceElement.fileName;
CORBA::Long lineNumber= stackTraceElement.lineNumber;
CORBA::WChar* w_class_name = new CORBA::WChar[className.value.length()];
w_class_name = TSCgetWString( className.value );
CORBA::WChar* w_method_name = new CORBA::WChar[methodName.value.length()];
w_method_name = TSCgetWString( methodName.value );
CORBA::WChar* w_file_name = new CORBA::WChar[fileName.value.length()];
w_file_name = TSCgetWString( fileName.value );
printf("at ");
my_print_wstring(w_class_name);
printf(".");
my_print_wstring(w_method_name);
printf("(");
my_print_wstring(w_file_name);
printf(":%d)\n",lineNumber);
}
}catch(TSC::java::rmi::RemoteEx &e){
// EJB application throws a Java runtime exception
// EJB application throws a Java system-defined error
// EJB application throws a java.rmi.RemoteException
TSC::java::rmi::RemoteException& ex_val = e.value;
// Output a detail message
printf("detailMessage:");
::TSC::TSCWStringValue& detailMessage = ex_val.detailMessage;
CORBA::WChar* w_detail_msg = new CORBA::WChar[detailMessage.value.length()];
w_detail_msg = TSCgetWString(detailMessage.value );
my_print_wstring(w_detail_msg);
printf("\n");
// Output a stack trace
::TSC::java::lang::seq1_StackTraceElement& stackTrace = ex_val.stackTrace;
CORBA::ULong len = stackTrace.value.length();
:TSC::java::lang::StackTraceElement stackTraceElement;
for ( int i = 0 ; i < len ; i ++ ) {
stackTraceElement = stackTrace.value [ i ] ;
::TSC::TSCWStringValue& className = stackTraceElement.declaringClass;
::TSC::TSCWStringValue& methodName= stackTraceElement.methodName;
::TSC::TSCWStringValue& fileName = stackTraceElement.fileName;
CORBA::Long lineNumber= stackTraceElement.lineNumber;
CORBA::WChar* w_class_name = new CORBA::WChar[className.value.length()];
w_class_name = TSCgetWString( className.value );
CORBA::WChar* w_method_name = new CORBA::WChar[methodName.value.length()];
w_method_name = TSCgetWString( methodName.value );
CORBA::WChar* w_file_name = new CORBA::WChar[fileName.value.length()];
w_file_name = TSCgetWString( fileName.value );
printf("at ");
my_print_wstring(w_class_name);
printf(".");
my_print_wstring(w_method_name);
printf("(");
my_print_wstring(w_file_name);
printf(":%d)\n",lineNumber);
}
} catch(TSCSystemException& se) {
// EJB application throws a Java system-defined error
// EJB application throws a CORBA system exception
// Invocation of the EJB application fails
printf(ERR_FORMAT,
se.getErrorCode(), se.getDetailCode(),
se.getPlaceCode(), se.getCompletionStatus(),
se.getMaintenanceCode1(), se.getMaintenanceCode2(),
se.getMaintenanceCode3(), se.getMaintenanceCode4());
} catch(UserExcept& se) {
// EJB application throws a CORBA user exception
printf("UserExcept\n");
}(b) Example of referencing exception information with Java
The following shows an example of referencing exception information with Java code.
// Function that performs byte[]-to-char[] conversion to obtain String
private static String myString(byte[] barray) {
char[] carry = new char[barray.length/2];
for ( int i=0;i<carry.length;i++) {
carry[i] = (char)((( barray[i*2]& 0xff) << 8 ) | barray[(i*2)+1]& 0xff) ;
}
return new String(carry);
}
public static void main(String[] args) {
try {
// Processing to invoke an EJB application (omitted)
}catch(jp.co.Hitachi.soft.test.MsgServerEx ex) {
// EJB application throws a Java user-defined exception (MsgServerException)
jp.co.Hitachi.soft.csc.msg.message.reception.MsgServerException testException = ex.value;
// Output a detail message
System.out.println ("detailMessage:"+ myString(testException.detailMessage);
// Output the m_case member variable
System.out.println ("m_case:%d"+ testException .m_case);
// Output a stack trace
TSC.java.lang.StackTraceElement[] stackElements= testException.stackTrace.value;
java.lang.StringBuffer stb = new java.lang.StringBuffer();
for (int i=0;i<stackElements.length;i++) {
stb = stb.append("at ");
stb = stb.append(myString(stackElements[i].declaringClass.value));
stb = stb.append(".");
stb = stb.append(myString(stackElements[i].methodName.value));
stb = stb.append("(");
stb = stb.append(myString(stackElements[i].fileName.value));
stb = stb.append(":");
stb = stb.append(stackElements[i].lineNumber);
stb = stb.append(")");
stb = stb.append("\n");
}
System.out.println(stb.toString());
}catch(TSC.java.rmi.RemoteEx ex) {
// EJB application throws a Java runtime exception
// EJB application throws a Java system-defined error
// EJB application throws a java.rmi.RemoteException
TSC.java.rmi.RemoteException testException = ex.value;
// Output a detail message
System.out.println ("detailMessage:"+ myString(testException.detailMessage);
// Output a stack trace
TSC.java.lang.StackTraceElement[] stackElements= testException.stackTrace.value;
java.lang.StringBuffer stb = new java.lang.StringBuffer();
for (int i=0;i<stackElements.length;i++) {
stb = stb.append("at ");
stb = stb.append(myString(stackElements[i].declaringClass.value));
stb = stb.append(".");
stb = stb.append(myString(stackElements[i].methodName.value));
stb = stb.append("(");
stb = stb.append(myString(stackElements[i].fileName.value));
stb = stb.append(":");
stb = stb.append(stackElements[i].lineNumber);
stb = stb.append(")");
stb = stb.append("\n");
}
System.out.println(stb.toString());
} catch(TSCSystemException tsc_se) {
// EJB application throws a Java system-defined exception
// EJB application throws a CORBA system exception
// Invocation of the EJB application fails
System.out.println(tsc_se);
} catch(UserExcept tsc_se) {
// EJB application throws a CORBA user exception
System.out.println("catch" + tsc_se.value);
}