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 - C# | Microsoft Learn
Goodluck!


