Try Catch doesn't work in custom activity

tl:dr - How can I include a working try-catch in my Uipath custom activity?

I am trying to incorporate a Try Catch in my C# code to handle a particular exception. Here is how my code looks:

                xlsBook.ExportAsFixedFormat(paramExportFormat, pDFFilePath);

catch (System.Runtime.InteropServices.COMException e)
                if (e.Message.Contains("Document not saved"))
                    throw new System.Exception("PDF Document not saved. Check if the document is currently open");
                    throw e;

Now this executes perfectly when I test the code in visual studio. It catches the exception “Document not saved” and instead throws the more detailed error message I have defined.

However, when I export this package and run it on UiPath as a custom activity, it still throws the default exception rather than execute the try-catch:

Any idea how I can resolve this?

Since this is a somewhat advanced topic, tagging some awesome folks who have already built custom activities: @lakshman @balupad14 @DanielMitchell @Palaniyappan


Perhaps you should check it with debug in Visual Studio.
Can you set breakpoint your source code in VS and try the following steps?

You can check if try-catch works and content of e.Message etc.



Embarrassed to say this, but there was a silly mistake in my code that caused this bug. In the code I used to generate the .dll for the custom activity, I had executed this line of code once above the try-catch:

xlsBook.ExportAsFixedFormat(paramExportFormat, pDFFilePath)

As a result, the error was getting triggered before entering the try itself.

A hat-tip to @Yoichi. I used his suggestion to debug within Visual Studio and resolve this problem. Thank you!

1 Like

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