0

I fully accept that this is essentially a repeat of question of Catching custom exception in c# That question is closed, so I hope to rephrase it as I am having the same problem.

I have a class that can be summarised thus..

[Serializable()]
public class DataFile : ISerializable
{
    public DataFile()
    {
        // Data structures
    }
    
    public DataFile(SerializationInfo info, StreamingContext ctxt) : this()
    {
        if(true)
        {
            throw new VersionNotFoundException();
        }
    
        // Load data
    }
    
    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {
        // Save data 
    }
}

In my MainForm, I have a method that constains code equivilant to..

private  DataFile Data;
private string CurrentFile = "C:\myfile.xyz";

private void LoadData()
{
    try
    {
        using (Stream stream = File.Open(CurrentFile, FileMode.Open))
            Data = (DataFile)new BinaryFormatter().Deserialize(stream);
    }
    catch (VersionNotFoundException e)
    {
         // never gets here
    }
    catch (Exception e)
    {
        // VersionNotFoundException gets caught here as an inner exception
    }
}

My question(s)

Why would the VersionNotFoundException not get caught in the "catch (VersionNotFoundException e)" section (have I not added it to the top of the exception stack)? What am I doing wrong and how do I fix it? Why/how am I making an 'inner' exception and how do I stop it?

damichab
  • 162
  • 10
  • https://dotnetfiddle.net/KWI1ac Works for me. is one of these in a .netStandard library? – TheGeneral Jul 10 '20 at 04:29
  • I think it is all standard .Net code. – damichab Jul 10 '20 at 04:48
  • Standard .NET and .NET Standard are different things. .NET Standard is like the old PCL (Portable Class Library) and is made for libraries that the .NET Core and the .NET Framework can use, as otherwise you couldn't share code between them – MindSwipe Jul 10 '20 at 04:50
  • Short answer is "I don't know". I use VS Community with pretty much standard settings and all my own code. This is a C# project with a UWP wrapper as an installer. – damichab Jul 10 '20 at 04:58
  • @TheGeneral, Just had a look at your link, and the way you have it, it does work. But not apparently from a DIFFERENT class. – damichab Jul 10 '20 at 05:09
  • You can check by right clicking on your project in VS, choose Properties and under the Application tab you'll see "Target framework" – MindSwipe Jul 10 '20 at 05:10
  • @MindSwipe its an inner exception :/ i completely overlooked the comment or what the serializer would throw – TheGeneral Jul 10 '20 at 05:10
  • Please directly explain "VersionNotFoundException gets caught here as an inner exception". It sounds like the code is throwing something like a TargetInvocationException, which sets its InnerException property or something like that. – Kit Jul 10 '20 at 05:12
  • Interesting that it doesn't work, making a cut down version of your example gives me [this](https://dotnetfiddle.net/TOWbFH) and it works – MindSwipe Jul 10 '20 at 05:13
  • 1
    @JQSOFT - yes thanks, I tried all sorts of things. The answer was to check for an inner exception. I did not know how to handle this, now I do! – damichab Jul 10 '20 at 05:52

1 Answers1

3

I was scratching my head with this and completely missed the comment.

// VersionNotFoundException gets caught here as an inner exception

You cannot catch inner exceptions like this, however you can use when in C#6 or later

try
{
   
}
catch (Exception e) when (e.InnerException is VersionNotFoundException e2) 
{
   Console.WriteLine(e2.Message);
}
catch (Exception e)
{
  
}

Demo here

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • That might be the comment I am looking for. But how do I stop it from being an inner exception in the first place - or is just the way it is? – damichab Jul 10 '20 at 05:11
  • 3
    @damichab you don't have any control over it, its the serizaliser that throws it – TheGeneral Jul 10 '20 at 05:11
  • Yep. This answer. It matches my comment on the question... might do to check what the outer exception is wrapping the inner though to make the catch even more specific. – Kit Jul 10 '20 at 05:16
  • @TheGeneral: Well that worked. I was tackling the probem all wrong - thanks. – damichab Jul 10 '20 at 05:16