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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:44:53+00:00 2026-05-31T07:44:53+00:00

I am trying to override the System.Exception class with my own exception class. I

  • 0

I am trying to override the System.Exception class with my own exception class. I would like to add the parameter “code”.

I would want to do this:

throw new MyException(code, "message");

Then catch the exception using something like this:

catch (MyException e) {
    Console.WriteLine(e.code)
}

Update: The problem was that I needed to catch the exception as “(MyException e)” instead of “(Exception e)”

This is my code so far:

public class MyException : System.Exception
{
    public String ErrorCode = "";

    public MyException() : base()
    {
    }

    public MyException(string message, string code) : base(message)
    {
        this.ErrorCode = code;
    }
    public MyException(string message, Exception inner, string code) : base(message, inner)
    {
        this.ErrorCode = code;
    }
}

This gives me an error that “System.Exception does not contain a definition for ‘ErrorCode’…”. What am I doing wrong?

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

    I have test your code,the exception class is right,but the catch is wrong.My test code is this.

            catch (MyException ex)
            {
                Console.WriteLine(ex.ErrorCode);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying a very simple override. I just want to add one line
I'm trying to decrypt an encrypted image in Android using this code: public class
I have this little bit of code: using System; using System.Web.Mvc; public class SecureFilter
I'm trying to create a child class of Android.App.Application to override OnCreate(), but I
this the code in which i am trying to demonstrate nested lock problem, import
I am trying to use the ImageLoader class from libs-for-android (http://code.google.com/p/libs-for-android/), and am getting
I'm trying to implement logging system into OSGI bundle. This is the bundle activator:
I'm getting a InvalidOperationException when trying to run this code: database.Produkts.SingleOrDefault<Produkt>(x => x.Kod ==
Trying to override a tostring in one of my classes. return string.Format(@ name =
I'm trying to override the equality (==) operator in C# to handle comparing any

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.