Best Way To Catch Exception (With Message) From Invoke Code Activity?

What is the best way to catch Exception from Invoke Code activity?

When there is an error inside Invoke Code activity, the exception message will be “An Exception has been thrown by the target of invocation” and this error is very vague and hard to find the bug because of its very generic error message.

Right now, I am doing something like

Try-Catch Activity
Try {
Invoke Code [
… … Arguments: outException / (direction: out) / (Type: System.Exception)
… … try
… … {
… … … Do some work
… …}
… …catch(exception ex)
… …{
… … …outException = ex;
… …}
…]
If activity: (outException IsNot Nothing)
… … … throw activity: outException.Message
}
catch
{
… …
}

This is too much work… What is the best way to catch the error message from within Invoke Code?

I usually avoid using the invoke code activity unless it is absolutely necessary. Are you 100% sure that the error message is being changed? Common ways that error message can occur when you try using a method that doesn’t exist (or wasn’t imported/referenced within your code), or if there is a namespace mismatch.

If you are sure it is changing the error message, then the easiest way to get the real error may be to write the try-catch in the code itself rather than surrounding the invoke code activity with it. Then when the error occurs save the message/source/whatever you want as a string variable. Then in your main workflow you can validate the invoke code was successful by checking if String.IsNullOrEmpty(errorString) or something similar

Okay so you mean I should use “out” directed variable from the code, right? to get the error message, if there is an error.
I am kind of doing the same thing, just outside the Invoke Code. I am using a variable of type Exception getting “out” from the code, and I store it in a variable for using it later.

Since UiPath spreads out everything, when you write a complicated logic, your UiPath code is very difficult to read. So many sequence layers and you cannot even see much of the code in one screen. Also, UiPath doesn’t have “else-if”, so if you want multiple else-ifs, you would have to use many “if” statements, which makes code even more difficult to read.
So whenever there is a lot of logic or if I think there will be too many activities, reducing the readability, I try to use Invoke Code as it is MUCH easier to read, and a lot of code lines can be seen in one screen.

Hi @tomato25,

This is a great question! I was wondering about the same thing. There seems to be no good way currently to write custom exception messages using Try-Catches within an Invoke Code activity.

As you have mentioned, one way to do this is to catch the generic exception thrown by the workflow and raise a custom exception - a workaround that isn’t suitable in several instances.

Does anybody else have a more elegant solution here? @loginerror @Luiza

Update: When I use Invoke Code within a custom activity and invoke it by using a library, even @tomato25’s workaround doesn’t work. It always only throws the generic exception message “An Exception has been thrown by the target of invocation”.

Hi all! The exception thrown from within the Invoke Code activity is passed as an InnerException.

6 Likes