0

I have been trying to load an excel file using the following code -

_workbook = await Task.Run(() =>
            {
               return _excelApp.Workbooks.Open(fileName);
            }).ConfigureAwait(false);

If I provide a non-existent file name in the fileName parameter, it's throwing a generic COMException with the following error code - 0x800a03ec

According to the Interop documentation, the .NET runtime should convert the HResult to a specific exception - https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.comexception?view=net-5.0

For this specific case, I was expecting to get a FileNotFoundException instead of the generic COMException. But since the HResult is not FILE_NOT_FOUND, its throwing the generic COMException. My question is why the interop is returning a wrong HResult? Is there anything wrong I have done here or its a bug in Excel.Interop?

Rasheduzzaman Sourov
  • 1,375
  • 2
  • 15
  • 36
  • 1
    It is not "wrong", error codes that start with 0x800a are used to shuttle an application-specific error back to your app. Next four hex digits are that error code, so you'd google "excel error 1004" to find hits. Yup, that happens a lot. – Hans Passant Mar 08 '21 at 09:29

1 Answers1

1

No, the documentation does not state that. It says:

The common language runtime transforms well-known HRESULTs to .NET Framework exceptions...

An arbitrary Excel HRESULT of 0x800a03ec is not "well-known", so .NET has no idea what to do with it, hence it remains a COMException.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138