Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6208715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:51:19+00:00 2026-05-24T05:51:19+00:00

What are the possible exceptions that can be thrown when XDocument.Load(XmlReader) is called? It

  • 0

What are the possible exceptions that can be thrown when XDocument.Load(XmlReader) is called? It is hard to follow best practices (i.e. avoiding generic try catch blocks) when the documentation fails to provide crucial information.

Thanks in advance for your assistance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:51:20+00:00Added an answer on May 24, 2026 at 5:51 am

    MSDN says: The loading functionality of LINQ to XML is built upon XmlReader.Therefore, you might catch any exceptions that are thrown by the XmlReader. Create overload methods and the XmlReader methods that read and parse the document.

    http://msdn.microsoft.com/en-us/library/756wd7zs.aspx
    ArgumentNullException and SecurityException

    EDIT: MSDN not always says true. So I’ve analyzed Load method code with reflector and got results like this:

    public static XDocument Load(XmlReader reader)
    {
        return Load(reader, LoadOptions.None);
    }
    

    Method Load is calling method:

    public static XDocument Load(XmlReader reader, LoadOptions options)
    {
        if (reader == null)
        {
            throw new ArgumentNullException("reader"); //ArgumentNullException
        }
        if (reader.ReadState == ReadState.Initial)
        {
            reader.Read();// Could throw XmlException according to MSDN
        }
        XDocument document = new XDocument();
        if ((options & LoadOptions.SetBaseUri) != LoadOptions.None)
        {
            string baseURI = reader.BaseURI;
            if ((baseURI != null) && (baseURI.Length != 0))
            {
                document.SetBaseUri(baseURI);
            }
        }
        if ((options & LoadOptions.SetLineInfo) != LoadOptions.None)
        {
            IXmlLineInfo info = reader as IXmlLineInfo;
            if ((info != null) && info.HasLineInfo())
            {
                document.SetLineInfo(info.LineNumber, info.LinePosition);
            }
        }
        if (reader.NodeType == XmlNodeType.XmlDeclaration)
        {
            document.Declaration = new XDeclaration(reader);
        }
        document.ReadContentFrom(reader, options); // InvalidOperationException
        if (!reader.EOF)
        {
            throw new InvalidOperationException(Res.GetString("InvalidOperation_ExpectedEndOfFile")); // InvalidOperationException
        }
        if (document.Root == null)
        {
            throw new InvalidOperationException(Res.GetString("InvalidOperation_MissingRoot")); // InvalidOperationException
        }
        return document;
    }
    

    Lines with exceptions possibility are commented

    We could get the next exceptions:ArgumentNullException, XmlException and InvalidOperationException.
    MSDN says that you could get SecurityException, but perhaps you can get this type of exception while creating XmlReader.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Java requires that you catch all possible exceptions or declare them as thrown in
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Does anyone know if there is any documentation that describes all the possible exceptions
Is it possible to log result,that method returned with annotation? or specific Exceptions,that was
Question 1: Is is possible to throw an exception that will not be caught
Possible Duplicate: When to choose checked and unchecked exceptions When should I create a
Possible Duplicate: catch exception by pointer in C++ I always catch exceptions by value.
Is it possible to test for multiple exceptions in a single JUnit unit test?
Is it possible to log WCF service exceptions? I have added in the app.config.
When throwing exceptions, I often pass in a formatted string that exposes details about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.