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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:25:06+00:00 2026-06-06T15:25:06+00:00

I want to test the following code: private bool TestException(Exception ex) { if ((Marshal.GetHRForException(ex)

  • 0

I want to test the following code:

private bool TestException(Exception ex)
{
    if ((Marshal.GetHRForException(ex) & 0xFFFF) == 0x4005)
    {
        return true;
    }
    return false;
}

I’d like to set up the Exception object somehow to return the correct HResult, but I can’t see a field in the Exception class which allows this.

How would I do this?

  • 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-06-06T15:25:08+00:00Added an answer on June 6, 2026 at 3:25 pm

    I found three ways to do this:

    1. Use the System.Runtime.InteropServices.ExternalException class, passing in the error code as a parameter:

      var ex = new ExternalException("-", 0x4005);
      

      Thanks to @HansPassant for his comment explaining this.

    2. Pass a mock exception using inheritance to access a protected field:

      private class MockException : Exception
      {
          public MockException() { HResult = 0x4005; }
      }
      
      var ex = new MockException();
      
    3. Use .NET Reflection to set the underlying field:

      BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
      FieldInfo hresultFieldInfo = typeof(Exception).GetField("_HResult", flags);
      
      var ex = new Exception();
      hresultFieldInfo.SetValue(ex, 0x4005);
      

    Passing any one of these exceptions to the method in the question, will result in that method returning true. I suspect the first method is most useful.

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

Sidebar

Related Questions

I have following code I want to test: public class MessageService { private MessageDAO
How I'm stuck with writing a test for the following code. I want to
I want to enforce on my code base immutable rule with following test [TestFixture]
I have the following (here simplified) code which I want to test with FakeItEasy
i want the following functionality. input : this is test <b> bold text </b>
I've set up the following for loop to accept 5 test scores. I want
I have the following XML <title> This is a <highlight>test</highlight> thanks. </title> and want
I want to test my part of code that returns the users password question.
I want to access a model attribute in Javascript. I use the following code:
I have the following code public ClassToTest : IClassToTest { private readonly DBRepository rep;

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.