Not sure how much of the code is necessary here. I'm trying to create a try block that doesn't include all of my code for handling the XML file. If I can't read the XML file, I get an UnauthorizedAccessException exception here:
XmlReader xmlIn = XmlReader.Create(configFile, settings);
But then I have all of my code that reads the XML and because the xmlIn is in the Try block, the rest of the code in my method can't see it. I thought about maybe creating a default XmlReader:
XmlReader xmlIn = null;
Before the try block and just throw the initialization in the try block, but the method is supposed to return a custom object that contains some strings and that causes the code that is supposed to run after that part to throw a NullReferenceException exception.
I searched for a while and the best I could come up with was the following that suggested that it's better to handle the exception, rather than testing access to the file beforehand, which is what I was thinking to try: how can you easily check if access is denied for a file in .NET?
So I'm kind of stuck in the middle with the proper way to handle this code that I thought would be pretty trivial. Do I just throw everything in the method into a try block, use the try in the part of my code that calls this method, or is there a better way to handle this?