Tech Tip: Handling the “Exception has been thrown by the target of an invocation” error in SSIS

Hadi Fadlallah
Tech Blog
Published in
1 min readFeb 26, 2023
Photo by Elisa Ventur on Unsplash

When executing the SSIS package the Script Task throws the following exception:

Exception has been thrown by the target of an invocation.
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[]
arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

Which is meaningless. How can i read the real error message?

Reading the real error message

Exception has been thrown by the target of an invocation.

Is a general exception that is thrown by Script Task when an error occurred. To read the main error message you can add a try catch clause into your code and use Dts.FireError() method to throw the real exception.

public void Main()
{
try{

//...write your code here

Dts.TaskResult = (int)ScriptResult.Success;

}catch(Exception ex){

Dts.FireError(0,"An error occured", ex.Message,String.Empty, 0);
Dts.TaskResult = (int)ScriptResult.Failure;

}


}

--

--

Hadi Fadlallah
Tech Blog

I write about data engineering, data management, SQL, and anything related to data. https://thedataengineer.blog