#1 2009-12-07 14:44:50

Elnourso
Member
Registered: 2009-11-09
Posts: 23

Catch Java Exceptions

Hello all,

I desperately try to get a Java (SQL) Exception to be caught by Talend.
I read many posts which say "easy, just put a tLogCatcher". Of course, that won't work...

I tried about anything, without luck (and this should be soooo trivial).

(see picture)
I want to catch any exception/tDie...
This leads to a tJavaRow which uses a message I build with a tMap to put it in a context variable (context.rejectedMessages; String) and sets another context variable to specify it failed.

Code:

context.isRejected = true;
context.rejectedMessages += input_row.message;

I tried with a
- tLogCatcher : catches all it can (Java Exception, tDie, tWarn); When I debug, the message in TalendException.printStackTrace() is not there when the component is looped for its messages (???)
(from the generated code)

Code:

    private class TalendException extends Exception {
        private java.util.Map<String, Object> globalMap = null;
        private Exception e = null;
        private String currentComponent = null;

        private TalendException(Exception e, String errorComponent,
                final java.util.Map<String, Object> globalMap) {
            this.currentComponent = errorComponent;
            this.globalMap = globalMap;
            this.e = e;
        }

        @Override
        public void printStackTrace() {
            if (!(e instanceof TalendException || e instanceof TDieException)) {
                globalMap.put(currentComponent + "_ERROR_MESSAGE", e
                        .getMessage());
                System.err
                        .println("Exception in component " + currentComponent);
            }
            if (!(e instanceof TDieException)) {
                if (e instanceof TalendException) {
                    e.printStackTrace();
                } else {
                    e.printStackTrace();
                    e.printStackTrace(errorMessagePS);
                }
            }
            if (!(e instanceof TalendException)) {
                try {
                    for (java.lang.reflect.Method m : this.getClass()
                            .getEnclosingClass().getMethods()) {
                        if (m.getName().compareTo(currentComponent + "_error") == 0) {
                            m.invoke(TX_SPAccoContractSupplements2TOST90.this,
                                    new Object[] { e, currentComponent,
                                            globalMap });
                            break;
                        }
                    }

                    if (!(e instanceof TDieException)) {
                        tLogCatcher_1.addMessage("Java Exception",
                                currentComponent, 6, e.getClass().getName()
                                        + ":" + e.getMessage(), 1);
                        tLogCatcher_1Process(globalMap);
                    }
                } catch (java.lang.SecurityException e) {
                    this.e.printStackTrace();
                } catch (java.lang.IllegalArgumentException e) {
                    this.e.printStackTrace();
                } catch (java.lang.IllegalAccessException e) {
                    this.e.printStackTrace();
                } catch (java.lang.reflect.InvocationTargetException e) {
                    this.e.printStackTrace();
                } catch (TalendException e) {
                    // do nothing
                }

            }
        }
    }

It enters the first "if" and prints out the exception trace; then it enters the "if (!(e instanceof TalendException)) " , finds the "_error" method and runs it;
then in "if (!(e instanceof TDieException))" it adds the java exception ("tLogCatcher_1.addMessage(....)"). Pretty cool, huh.
But at the next line ("tLogCatcher_1Process(globalMap);"), no message is found in this method's loop ==> nothing gets fired  :'(

- a PostJob tJava which checks for globalMap.get("xxxx_ERROR_MESSAGE")  without any luck  :

Code:

//check for errors... once again it is hardcoded...
if (globalMap.get("tPrejob_1_ERROR_MESSAGE") != null) {
    context.isRejected = true;
    context.rejectedMessages += "\n" + (String)globalMap.get("tPrejob_1_ERROR_MESSAGE");        
}

if (globalMap.get("tMSSqlConnection_1_ERROR_MESSAGE") != null) {
    context.isRejected = true;
    context.rejectedMessages += "\n" + (String)globalMap.get("tMSSqlConnection_1_ERROR_MESSAGE");        
}

if (globalMap.get("tDB2Connection_1_ERROR_MESSAGE") != null) {
    context.isRejected = true;
    context.rejectedMessages += "\n" + (String)globalMap.get("tDB2Connection_1_ERROR_MESSAGE");        
}

if (globalMap.get("tMSSqlInput_1_ERROR_MESSAGE") != null) {
    context.isRejected = true;
    context.rejectedMessages += "\n" + (String)globalMap.get("tMSSqlInput_1_ERROR_MESSAGE");        
}

if (globalMap.get("tDB2Output_1_ERROR_MESSAGE") != null) {
    context.isRejected = true;
    context.rejectedMessages += "\n" + (String)globalMap.get("tDB2Output_1_ERROR_MESSAGE");    
}

I'm currently desperate.

That job is called by another one.
I see a stack trace of the exception in the console output.

My parent job passes its whole context (works : otherwise the job could not connect to the DB's).


==> is this a bug?
Thank you for any quick (constructive) answer smile

Cheers,

Offline

#2 2009-12-07 15:29:55

Elnourso
Member
Registered: 2009-11-09
Posts: 23

Re: Catch Java Exceptions

Learning a bit more about the "problem".

When I debug, I check the tLogCatcher_1.getMessages() to see if my message is filled. Appearently, this does remove the messages that are read (correct me if I'm wrong)
==> when the loop uses getMessages, there is nothing anymore...

OK

==> my problem is the one that context variables need to be passed to the parent job via a tBufferOutput
http://www.talendforge.org/forum/viewtopic.php?id=7604

I'll add the workaround to  my 25 jobs + my main big job which runs them all (and it's not quickly done in Studio, I can tell you).

Feature to add:
- enable IN/OUT for context variables.
-  a better documentation/FAQ/........... the info is so unfindable.

Cheers,


PS : sorry for the bad mood, but I am losing so much time in such things since I use Talend that it drives me insane

Offline

Board footer

Powered by FluxBB