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 849005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:02:00+00:00 2026-05-15T07:02:00+00:00

For the love of all things holy, how do you distinguish between different exception

  • 0

For the love of all things holy, how do you distinguish between different “exception flavors” within the predefined .NET exception classes?

For example, a piece of code might throw an XmlException under the following conditions:

  • The root element of the document is NULL
  • Invalid chars are in the document
  • The document is too long

All of these are thrown as XmlException objects and all of the internal “tell me more about this exception” fields (such as Exception.HResult, Exception.Data, etc.) are usually empty or null.

That leaves Exception.Message as the only thing that allows you to distinguish among these exception types, and you can’t really depend on it because, you guessed it, the Exception.Message string is glocabilized, and can change when the culture changes. At least that’s my read on the documentation.

Exception.HResult and Exception.Data are widely ignored across the .NET libraries. They are the red-headed stepchildren of the world’s .NET error-handling code. And even assuming they weren’t, the HRESULT type is still the worst, downright nastiest error code in the history of error codes. Why we are still looking at HRESULTs in 2010 is beyond me. I mean if you’re doing Interop or P/Invoke that’s one thing but… HRESULTs have no place in System.Exception. HRESULTs are a wart on the proboscis of System.Exception.

But seriously, it means I have to set up a lot of detailed specific error-handling code in order to figure out the same information that should have been passed as part of the exception data. Exceptions are useless if they force you to work like this. What am I doing wrong?

EDIT. A DAY LATER.

Thanks for all the comments and responses. I’m learning a general lesson here (“error messages suck”) even if I haven’t quite solved the specific problem. Here’s the specific scenario I’m deaing with.

Our application consumes XML files produced by a third party. These XML files sometimes contain illegal characters that really have no business being in an XML file. These illegal chars cause a (validating) XmlReader to blow up with an “illegal char on line X” exception”. We have to process these files, however; we can’t simply tell the user, “sorry, that file doesn’t conform to the official XML spec.” Our users don’t even really know what XML is.

Microsoft has an official (and quite strange to me) recommendation in this case (the case where an XML document contains illegal characters): to load the file into a stream, iterate to the specific line containing the error (as provided, ironically, in the XmlException object), and to custom-replace the offending char with a legal one. Then try loading the doc into the validating XmlReader again, and seeing if it blows up. Here’s the Knowledge Base article describing the technique.

So fine, we’re doing that, and it works well enough. The problem is that sometimes these XML files we get are malformed in other ways: they might be empty, they might be missing a closing tag, etc. So if you follow the MS recommendation, you’re actually hooking your “replace illegal chars” logic into the catch block where you catch the original exception thrown by the validating reader.

That’s fine if the exception is in fact an “illegal char” exception. But if it’s a “root element missing” or “missing closing tag” exception, you find that the MS technique to go in and replace the offending char itself blows up, because not only is there not an offending char, there are no chars at all, or there are, but they’re invalidly-formed XML, or whatever. At this point you’re in a doubly-nested catch-inside-a-catch, your hair’s turning gray and falling out, your eyes are crimson red with caffeine fatigue, and you’re questioning the sanity, let alone the utility, of using validating readers against real-world XML.

So what I need is a way of telling, in that initial catch(XmlException) block, whether this is a “root element missing” or a “invalid char” exception, so I can take the appropriate action. One thing we can’t do is prevent our users to open a document that contains a few invalid chars. We have to process those documents regardless, and I guess it’s looking like the only solution is to iterate through every char in the document ahead of time, viewing it as a text stream rather than as XML, find the illegal chars, replace them, then load the thing with the validating XmlReader and if it blows up, we know it’s not an illegal char exception, because we stripped illegal chars beforehand.

Before doing that I thought I’d at least ask 1) can we get better information out of the XmlException object and I was hoping somebody would tell me 2) “it’s okay to key off the XmlException.Message string. They say it’s localized but it’s not really. This string will be the same across all versions and cultures of Windows.”

But nobody has told me that.

  • 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-15T07:02:02+00:00Added an answer on May 15, 2026 at 7:02 am

    I totally agree with you in principle, but the number of instances where I really care about the exact reason the exception was being thrown (in my code) is pretty small.

    Using XmlException as an example, is there really some different behavior you would have in your code if the document was too long vs. if it had invalid characters?

    The only example I can think of where I’ve ever really cared was for SQL type exceptions where some errors can be recovered from (e.g. a lost database connection).

    ETA:

    If you are worried about the culture when keying off the error message, you could just set the current thread culture to the invariant culture while you are processing the document, then set it back to the original culture when you are done. That should ensure that the message will always be the same.

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

Sidebar

Related Questions

I'm quickly falling in love with ASP.NET MVC beta, and one of the things
I'd really love your help with this deciding whether the language of all words
OK I love Python's zip() function. Use it all the time, it's brilliant. Every
I love paredit. But there are a couple of things I hate, and have
One of the things I love about Visual Studio 2008 is the ability to
What's the best way implement MS SQL full-text search using all the normal things
  I've started working on a project that loads all of the different pages
I love things that are a power of 2. I celebrated my 32nd birthday
First of all, I love vim. I have been using vim for a few
I'm a long time user of the boost::smart_ptr library and love it. Like all

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.