5 Replies Latest reply on Feb 17, 2020 9:11 AM by Nico Heinze

    Failsession() when got an exception

    Daniel Dinca New Member

      Hello,

       

      I have no experience with Java Transformation and I want to fail a session when I got a exception, because one of my sessions throws a exception but the session successfully executed even if I have stop on errors 1.

       

      I searched for a solution and I found failsession() method but in the documentation says that I cannot use it in try/catch blocks and I wanned to use it in catch exception block. Can you please help me? How can I use it? I want my session to fail if I find a exception and eventualy show the error message.

       

      Thank you,

      Daniel

        • 1. Re: Failsession() when got an exception
          Nico Heinze Guru

          I would use the method incrementErrorCount( 1) and set the session to Stop On Errors = 1. That should work fine. And as far as I recall, incrementErrorCount() can be used anywhere in your code (in contrary to failSession()).

           

          Regards,

          Nico

          1 of 1 people found this helpful
          • 2. Re: Failsession() when got an exception
            Daniel Dinca New Member

            Hi Nico,

             

            Thank you for your answer. I can't test the code now because the java transformation works in the next test environement.

             

            I whould add this method in the catch Exception block as follows:

             

            Catch(Exception e)

            {

                      e.printStackTrace();

                      incrementErrorCount(1);

            }

             

            Do you think it should work?

             

            Thank you,

            Daniel

            • 3. Re: Failsession() when got an exception
              Krishna Lingamallu Seasoned Veteran

              Hi Daniel,

               

              Java transformation is a custom transformation, hence stop on errors will not catch the error of java code.

              In case if you want to make session fail, use the incrementErrorCount in java code.

               

              incrementErrorCount

              Increments the error count for the session. If the error count reaches the error threshold for the session, the sessionmapping fails.

               

              Use the following syntax:

              incrementErrorCount(int nErrors);

              The following table describes the parameter: 

                   
              Parameter Parameter Type Datatype Description
              nErrors

               

              Input Integer

              Number by which to increment the error count for the session.

              You can add the incrementErrorCount method to the Java code on any code entry tab except the ImportsImport Packages and FunctionsJava Expressions tabs.

              The following Java code shows how to increment the error count if an input port for a transformation has a null value:

              // Check if input employee id and name is null. 
              if (isNull ("EMP_ID_INP") || isNull ("EMP_NAME_INP"))
              {     
                   incrementErrorCount(1);
                   // if input employee id and/or name is null, don't generate a output row for this input row
                   generateRow = false;
              }

               

              Thanks,

              Krishna

              • 4. Re: Failsession() when got an exception
                Nico Heinze Guru

                Hi Daniel,

                 

                at this moment I can't answer with 100% confidence. The point is that a "code skeleton" is inserted by the JTX editor automatically when you click the Compile link. As far as I recall (my last examination in this respect is quite some time ago), this "code skeleton" already contains a try - catch combination, and this try block contains the code you enter on the On Every Row block. Which means that you could not provide your own try - catch block (because they should not be nested).

                I'll have to take a look at the current implementation first before I can answer this one. But don't take my answer as a "safe statement", I'm no member of Informatica R&D and thus cannot give any authoritative statements about this technical detail.

                 

                However, there's another point which you should be aware of. In your last question you have inserted this:

                  e.printStackTrace();

                That's an extremely bad idea. As pointed out in the FAQ article about the Java Transformation (to be found in Velocity, the Best Practices collection maintained by Informatica Professional Services, under the link Resources at the top of the Informatica Network home page) one should never try to use standard input or standard output of the JRE, these simply do not exist in the JTX.

                What you can do is to convert the stack trace into readable text using the StackTraceElement class (part of the Java standard in java.lang); each Exception is an instance of the class Throwable, and this class Throwable provides a method named getStackTrace() (returning an array of StackTraceElement objects), so you can simply invoke e.getStackTrace() to get such an array. Simply iterate through this array of StackTraceElement objects and use the toString() method for each one, then write this string to the session log using logInfo(). That's the safest and easiest way to get the whole stack trace of any Java exception in a JTX into the session log.

                 

                Regards,

                Nico

                • 5. Re: Failsession() when got an exception
                  Nico Heinze Guru

                  Again, this is no authoritative statement because I'm no member of Informatica R&D, but in 10.1.1 the JTX encloses the "On Every Row" block in a try{} statement; that means you may throw exceptions, but you can NOT handle them on your own in the Java code. No chance.

                  As far as I know the JTX will automatically catch all exceptions and print their contents in the session log file. There's no need to follow my (very short) suggestion about StackTraceElement and trying to implement something like this on your own, this is already done by the JTX.

                   

                  So in order to answer your initial question (from my side, once and for all):

                  Simply use

                      incrementErrorCount(1);

                  whenever you encounter some situation in your Java code where you have to abort the session. Alternatively you may use failSession() whenever you encounter a situation when continuing doesn't make sense.

                  Just be aware that you can NOT insert your own "catch" block of code in a JTX.

                   

                  Regards,

                  Nico