Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle exceptions for XmlReader.Create
    primarykey
    data
    text
    <p>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:</p> <pre><code>XmlReader xmlIn = XmlReader.Create(configFile, settings); </code></pre> <p>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:</p> <pre><code>XmlReader xmlIn = null; </code></pre> <p>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.</p> <p>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: <a href="https://stackoverflow.com/questions/265953/how-can-you-easily-check-if-access-is-denied-for-a-file-in-net">how can you easily check if access is denied for a file in .NET?</a></p> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload