Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
2 replies Latest Post - ‏2014-05-13T13:04:11Z by Samuele_F
Samuele_F
Samuele_F
6 Posts
ACCEPTED ANSWER

Pinned topic Exception Handling in Java

‏2014-05-05T11:28:20Z |

 Hi,

i wrote a generic java program to handle exceptions. The program logs in database some informations such as exception message, the stack trace, sql code in case of sql errors and others.
 
It is possible reuse the same source from an EGL program?
 
The argument of the java function is a generic object.
 
I tried to map the Java class as external type, but in catch block of egl program there is no way to "cast" the exception (i.e. AnyException) as object.
 

The following is an example of code that shows the desidered behaviour:

it is possible to obtain this behaviour?
ExternalType Object type JavaObject 
{ 
    packageName = "java.lang" 
}
end

ExternalType UtilityErroriET type JavaObject 
{
    packageName = "routine"
}
        static function logErrori(ex Object in);
end


program ProvaZeroDivision type BasicProgram {}
    
    // Use Declarations
    //use usePartReference;         
    // Constant Declarations
    const constantName string = "literalString";
    
    // Variable Declarations
    variableName string;
    
    function main()
        try
                result int = 5/0;
        
        onException(ex AnyException)
                SysLib.writeStderr(ex.message);
                //Call the external Java Functions
                UtilityErrori.logErrori(ex);
        end
        end
end
 
Thanks in advance
Updated on 2014-05-05T12:37:53Z at 2014-05-05T12:37:53Z by Samuele_F
  • HuangJiYong
    HuangJiYong
    88 Posts
    ACCEPTED ANSWER

    Re: Exception Handling in Java

    ‏2014-05-13T02:33:22Z  in response to Samuele_F

    EGL Java ExternalType does not accept record as a function parameter; AnyException is also a kind of record. If you only need to deal with the exception message id and message text, you can change the function signature to accept an int and a string. Check below code:

    ExternalType Object type JavaObject 
    { 
        packageName = "java.lang" 
    }
    end
    
    ExternalType UtilityErroriET type JavaObject 
    {
        packageName = "routine"
    }
            static function logErrori(messageId int in, message string in);
    end
    
    
    program ProvaZeroDevision type BasicProgram {}
        
        // Use Declarations
        //use usePartReference;         
        // Constant Declarations
        const constantName string = "literalString";
        
        // Variable Declarations
        variableName string;
        
        function main()
            try
                    result int = 5/0;
            
            onException(ex AnyException)
                    SysLib.writeStderr(ex.message);
                    //Call the external Java Functions
                    UtilityErroriET.logErrori(ex.messageID, ex.message);
            end
        end
    end
    

    Hope this helps.

    -Jiyong

    • This reply was deleted by Samuele_F 2014-05-13T13:03:38Z.
  • Samuele_F
    Samuele_F
    6 Posts
    ACCEPTED ANSWER

    Re: Exception Handling in Java

    ‏2014-05-13T13:04:11Z  in response to Samuele_F
    Thanks for your help!
    Finally I solved defining a new library and using JavaLib.invoke function to call external java program.
    In logErrori function of this library, I cast the exception to Java Object using keyword any:
     
    library LibErrori type BasicLibrary {}
        
            // Function Declarations
            function logErrori(ex anyException)
                    JavaLib.invoke("routine.UtilityErrori", "logErrori", ex as any);
            end
            
    end
    
    
     
    In the main program, I call logErrori method within every catch block:
     
    program ProvaZeroDivision type BasicProgram {}
        
            // Variable Declarations
            dsIndirizzo string;
            
            function main()
                    try
                            result int = 5/0;
                    onException(anyEx AnyException)
                            SysLib.writeStdout(anyEx.message);
                            SysLib.writeStdout(StrLib.formatTime(dateTimeLib.currentTime()));
                            LibErrori.logErrori(anyEx);
                            exit program(1);
                    end
            end
    end
    

     

    It works!
    Updated on 2014-05-13T13:05:50Z at 2014-05-13T13:05:50Z by Samuele_F