Home > database >  How can I have an exception show in debugging output that doesn't cause a "catch"
How can I have an exception show in debugging output that doesn't cause a "catch"

Time:11-16

This may be a basic question but I have not been able to find an answer from searching. I have code that is causing an exception to be written to the Output -> Debug window in Visual Studio. My try...catch is proceeding to the next line of code anyway. The exception is with a NuGet package.

Does this mean an exception is happening in the NuGet package and is handled by the Nuget package? How can I troubleshoot this further?

private void HandleStorageWriteAvailable(IXDocument doc)
{
    using IStorage storage = doc.OpenStorage(StorageName, AccessType_e.Write);
    {
        Debug.WriteLine("Attempting to write to storage.");

        try
        {
            using (Stream str = storage.TryOpenStream(EntityStreamName, true))
            {

                if (str is not null)
                {
                    try
                    {
                        string test = string.Concat(Enumerable.Repeat("*", 100000));
                        var xmlSer = new XmlSerializer(typeof(string));
                        xmlSer.Serialize(str, test);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Something bad happened when trying to write to the SW file.");
                        Debug.WriteLine(ex);
                    }
                }
                else
                {
                    Debug.WriteLine($"Failed to open stream {EntityStreamName} to write to.");
                }
            }

        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
    }
}

The exception happens on the line using (Stream str = storage.TryOpenStream(EntityStreamName, true)) when the exception happens the code proceeds to the next line not the catch.

Is this normal behaviour if that exception is being handled by something else? I've never seen this before.

CodePudding user response:

In general, a method called TrySomething will be designed so that it won't throw an exception, but return some sort of error code instead.

Check for example the Dictionary class : it has an Add method which can throw an ArgumentException if the key already exists, and a TryAdd method which instead just returns false.

Chances are, your IStorage implementation of TryOpenStream also has an OpenStream method, and the Try version is just a try/catch wrapper which outputs the error to the Console in case of error.

CodePudding user response:

How do you know it happens on that line? However there is a setting that enables breaking handled exception in "Exception Settings" dialog (Ctrl Alt E). For each type of exception you can control. Here is a link that explain how it works : https://docs.microsoft.com/en-us/visualstudio/debugger/managing-exceptions-with-the-debugger?view=vs-2022

  • Related