Monday, March 26, 2012

Package variables in case of an Exception

Hi there,

I may be blind, but I'm sure that I have seen a post about package variables 'ExceptionMessage'
and 'ExceptionCode' or somehow which are only available when an Exception occurs.
I cannot find it anywhere :(

If there isn't any then the question is: Can I read out the message and the code of an occurring Exception e.g. from a Script Component/ Custom Task which is controlled by an error constraint?

Can someone help?
Fridtjof

Fridtjof,

The best way to do this is place a TRY...CATCH block in your script component. In the catch block you can raise an OnError event containing the error message.

Look here for some useful stuff:

http://blogs.conchango.com/jamiethomson/archive/2005/08/08/1969.aspx

http://blogs.conchango.com/jamiethomson/archive/2005/10/10/2253.aspx

http://blogs.conchango.com/jamiethomson/archive/2005/11/03/2356.aspx

http://sqljunkies.com/WebLog/simons/archive/2005/12/15/17626.aspx

-Jamie

|||Jamie,

great contribution!

But you focussed on script component. What if I want to use a custom control flow task to handle the exception. There I don't have any input columns esp. error inputs.

My idea was that in case of an exception you could read the code and the message from the
package variable for any further handling, e.g. do some custom logging.

Fridtjof
|||

The same general approach (i.e. throw OnError events) should work - the syntax is just a little different in a script task that's all.

-Jamie

|||Jamie,

sorry, but this is not what I meant.

E.g. I have an Exec-SQL task A and a Custom Control Flow task named B.
A and B are connected via a red precedence constraint. A runs first and B second in case of
an Exception.

In B I want to read out the Exception Message (for custom logging).

This works for Data Flow Tasks like you said. But not for Control Flow as far as I see.

Sorry that I wasn't that precise but I was looking for both Control Flow and Data Flow...

Thanks
Fridtjof
|||

Every tasks that errors throws an error event.

The error will be available in the OnError eventhandler. You can do what you like with it in there.

-Jamie

|||I guess the OnError event is called only if the task itself raises an error.

The point was that, as described above, task A raises an exception and task B is then called via an error constraint. I want to evaluate e.g. an SQLException from an ExecSql Task in a subsequent custom task since I cannot do this in the ExecSQL Task itself.

Fridtjof
|||

Friedel wrote:

I guess the OnError event is called only if the task itself raises an error.
The point was that, as described above, task A raises an exception and task B is then called via an error constraint. I want to evaluate e.g. an SQLException from an ExecSql Task in a subsequent custom task since I cannot do this in the ExecSQL Task itself.
Fridtjof

I don't understand why putting the custom task ito the OnError eventhandler is not sufficient. I guess I just don't understand your scenario well enough!

-Jamie

|||OMG, I've got it!!!

You meant the Eventhandler of the package which is available at the Workbench!!!

I tried to implement an EventHandler in C# in the Custom task. How stupid :(

But now everything is great again.

Thanks for having this long thread talking at cross-purposes. Sorry for that.
Fridtjof

No comments:

Post a Comment