Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find exceptions thrown by OpenReadAsync method

Consider the following code:

using (IRandomAccessStream stream = await storageFile.OpenReadAsync())
{
    using (DataReader dataReader = new DataReader(stream))
    {
        uint length = (uint)stream.Size;
        await dataReader.LoadAsync(length);
        txtbox.Text = dataReader.ReadString(length);
    }
}

storageFile.OpenReadAsync may throw exception, System.IO.FileNotFoundException is one possible exception type. MSDN topic StorageFile.OpenReadAsync http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.storagefile.openreadasync doesn't contain list of exception types thrown by this method. How can I find this information from documentation? I can catch an Exception type, but this is poor programming practice.

like image 576
Alex F Avatar asked May 30 '26 08:05

Alex F


1 Answers

In cases where it is impossible to find all list of exceptions I usually use approach from VS SDK ErrorHandler.IsCriticalException:

try
{
    // ...
}
catch(Exception e)
{
    if (ErrorHandler.IsCriticalException(e))
    {
        throw;
    }

    // log it or show something to user
}

You can decompile the Microsoft.VisualStudio.Shell.11.0.dll to find the list of exceptions, which ErrorHandler defines as Critical:

  • StackOverflowException
  • AccessViolationException
  • AppDomainUnloadedException
  • BadImageFormatException
  • DivideByZeroException

In the case of Windows Runtime I think that it will be good also to verify some of the HResult values in Exception, like E_OUTOFMEMORY, E_ABORT, E_FAIL, and maybe something else.

Also I found that BugSense is awesome help for logging exceptions. I use it not only for unhandled exception, but also for situations like this, where I have no idea what this method can throw. It allows to send custom logging (including exceptions) with BugSenseHandler.Instance.LogException, so I just collect information about different kind of exceptions (including exceptions with some unexpected HResult) and make some improvements for my app in each release.

like image 158
outcoldman Avatar answered Jun 01 '26 23:06

outcoldman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!