Handling str2date() Error

Hi,

I would like to trap str2date() errors in my CTL code, however, for cases where the incoming format is unexpected then the whole graph assembly fails. I was hoping the transformOnError() function would handle this so I could perform some action (e.g. SKIP or write to a separate output port) but the whole job just terminates.

How can i trap this sort of error and introduce some sort of handling?

Thanks,
Paul

Hi Paul,

There are 2 options, how to handle it. One works only with the newest CloverDX release (5.6), the second even in older versions. But both should be used in interpreted mode of CTL.

Option 1: try-catch (since CloverDX 5.6.)

date mydate;
try{
 mydate = str2date( ... params...);
 .. some other code..
}catch(CTLException ex){
  .. other logic in case of data/format error.
}

Option 2: conditional fail expression

date mydate;
mydate = str2date(....params...) : 2000-01-01;  //notice the ":" operator

Thanks David! This worked for me.

Out of interest, what was it about this type of error which meant TransformOnError() or similar wasn’t possible?

Paul

Hello Paul,

Can’t really answer that… would need to see the whole transform() & transformOnError() function you have. In general, onError() is called when exception is raised in transform(). If your transformOnError() returns SKIP (constant), then current record is skipped and processing continues with the next one. If you return ALL, then whatever was already written to $out. is sent out (means can be partially updated output record). Of course, you can abort the whole processing inside transformOnError() if you return negative number or call raiseError() function.

Hope this explains it a bit.