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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:44:48+00:00 2026-05-24T03:44:48+00:00

try { object result = processClass.InvokeMethod(Create, methodArgs); } catch (Exception e) { // Here

  • 0
try
{
     object result = processClass.InvokeMethod("Create", methodArgs);
}
catch (Exception e)
{  
    // Here I was hoping to get an error code.
}

When I invoke the above WMI method I am expected to get Access Denied. In my catch block I want to make sure that the exception raised was indeed for Access Denied. Is there a way I can get the error code for it ? Win32 error code for Acceess Denied is 5.
I dont want to search the error message for denied string or anything like that.

Thanks

  • 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-24T03:44:49+00:00Added an answer on May 24, 2026 at 3:44 am

    You can use this to check the exception and the inner exception for a Win32Exception derived exception.

    catch (Exception e) {  
        var w32ex = e as Win32Exception;
        if(w32ex == null) {
            w32ex = e.InnerException as Win32Exception;
        }    
        if(w32ex != null) {
            int code =  w32ex.ErrorCode;
            // do stuff
        }    
        // do other stuff   
    }
    

    Starting with C# 6, when can be used in a catch statement to specify a condition that must be true for the handler for a specific exception to execute.

    catch (Win32Exception ex) when (ex.InnerException is Win32Exception) {
        var w32ex = (Win32Exception)ex.InnerException;
        var code =  w32ex.ErrorCode;
    }
    

    As in the comments, you really need to see what exception is actually being thrown to understand what you can do, and in which case a specific catch is preferred over just catching Exception. Something like:

      catch (BlahBlahException ex) {  
          // do stuff   
      }
    

    Also System.Exception has a HRESULT

     catch (Exception ex) {  
         var code = ex.HResult;
     }
    

    However, it’s only available from .NET 4.5 upwards.

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

Sidebar

Related Questions

I create a pointer-to-pointer of a class object and when I try to create
I'm trying to catch a ClassCastException when deserializing an object from xml. So, try
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
I sometimes get the following exception: COM object that has been separated from its
I try to run MATLAB code in Java with MATLAB Builder JA to get
I need to try to lock on an object, and if its already locked
i try to put a lock to a static string object to access to
When I try to display the contents of a LOB (large object) column in
try { ... } catch (SQLException sqle) { String theError = (sqle).getSQLState(); ... }
I try to display a Toast inside a AsyncTask. This first piece of code

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.