Exception Handling Query

I want to understand flow in which exceptions will be executed in catch block ?
For ex : I am trying to open excel file using workbook ‘read range’ activity and file is open, this will throw an exception.

In catch block, I have arranged exception in below manner -

  1. System excetion
  2. I/O exception

Which will be excuted first and what is reason for this?
This question was asked in interview, I really want to understand this concept.

Hi @ashwini.magar92,

Very good question. Definitely someone who has spent a lot of time in the Microsoft development ecosystem came up with this simple but tricky question.

Within UiPath and I am pretty sure in .net / PowerShell and C#:
System.IO.IOException will trigger first if the file cannot be opened as it is already opened / file not accessible to read.

But lets say the error was due to a wrong sheet name used, then the file opening function was successfully but the function to read data would fail. This would trigger the System.Exception

Answer to your question.

System.IO.IOException will be triggered.


Why did System.IO.IOException get the preference in this case?

I am almost certain that this is because .net / PowerShell / C# all know to choose the most appropriate exception for a given case. In the question you asked, we know that UiPath will not be able to read the file which means that if one has specified a very granular exception handler System.IO.IOException then this has to be the preferred exception handler.

We can test this hypothesis by using the same scenario with only a System.Exception catch block. The error gets caught in this case as well but if System.IO.IOException (granular exception handler) was present then it would get preference over System.Exception as this (System.Exception) is not granular but very broad exception handler.

Here is the xaml which you can refer. Both cases are tested here.
Test.xlsx (7.2 KB)
ExceptionHandlingOrder.xaml (10.0 KB)

Next time you get such an question. Take a moment to check what kind of error can be expected in the scenario and based on the options, look for the most granular exception handler block and ignore the very generic exception handler block. Hope this clears your doubts.

Useful reading (Things to Avoid When Throwing Exceptions): Creating and Throwing Exceptions | Microsoft Docs

Goodluck!

1 Like

@jeevith Thanks a lot for explanation, I understood the concept.
Just one more thing adding to above I want to understand, Whether the order in which exception is written inside catch block affets the execution ?
For ex:
If we write one order as below,

  1. first system exception and 2nd IO exception,
    and another order
  2. 2nd IO exception and first system exception

Whether there will be any change in execution order and if so what could be possible reason for it?

Hi @ashwini.magar92,

The order of the catch block will not affect the execution. The most granular exception block will be executed first. :slight_smile:
You can see it in the screenshot above, where I had System.Exception first but the execution directly went to System.IO.IOException.

1 Like

@jeevith Perfect, thanks a lot for your help.

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