Invoke activity from code (c#)

I want to throw a BusinessRuleException from inside Invoke Code, and this is as far as I get:
But how do I invoke the Throw?
The class does have an Execute() method, but it is protected. I might need to implement some other parent class?

Studio v2021.10.5

Hey @Tiramisu

Directly throw it please.

Throw new BusinessRuleException("message");

Hi @Nithinkrishna, unfortunately that doesn’t work. Instead of a BusinessRuleException, that would raise a TargetInvocationException. And I am not catching that in my Try/Catch for BusinessRuleException.

RemoteException wrapping System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.  ---> RemoteException wrapping UiPath.Core.BusinessRuleException: a box of kittens! 
   at UiPath.CodeRunner.UiPathCodeRunner_911d3fca55f64c20923bf9af4176b903.Run()
	--- End of inner exception stack trace ---
   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 UiPath.Activities.System.Utilities.InvokeCode.CompilerRunner.Run(Object[] args)
   at UiPath.Activities.System.Utilities.InvokeCode.NetCodeInvoker.Run(String userCode, List`1 inArgs, IEnumerable`1 imps, Object[] args)
   at UiPath.Core.Activities.InvokeCode.Execute(CodeActivityContext context)
   at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
   at System.Activities.ActivityInstance.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)
   at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

Kindly show the code please. Thanks

Just as we discussed

throw new BusinessRuleException("A box of kittens!");

@Tiramisu Instead of business rule exception can you try giving the specific exception and see how it goes



True, but that is gonna give me other problems when integrating into an existing framework and orchestrator.
I would rather just solve the problem of throwing a BusinessRuleException

1 Like

Hey @Tiramisu

It’s due to the exception got wrapped with a top layer by UiPath.

If you check inner exception you will be having the BRE details. You need to catch a normal exception when the invoke code is in Try block and check the inner exception type in the catch block whether it’s BRE to do your intended steps.

Hope that’s clear.


Hey @Tiramisu

Also attaching a sample project for your reference. (2.3 KB)


Hi @Nithinkrishna, that is an interesting workaround.
Can you explain that top layer wrapping that happens?

I’m still looking for a way to throw BusinessRuleException without hacks.

Hey @Tiramisu

This is not a hack actually. But this is how the .NET works !

Invoke Code activity is a source inside which a Business Exception is thrown by you → The activity itself is a .NET code which catches the exception you have thrown and throws it back again outside to the sequence as System Exception → Now in your sequence you would have used Try Catch with Exception eException e object will have first layer of System Exception thrown by Invoke Code and then Inner Exception inside it which holds the actual source BRE thrown inside by you.

Hope this helps.


This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.