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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:44:21+00:00 2026-05-10T14:44:21+00:00

Right, I know I am totally going to look an idiot with this one,

  • 0

Right, I know I am totally going to look an idiot with this one, but my brain is just not kicking in to gear this morning.

I want to have a method where I can say "if it goes bad, come back with this type of Exception", right?

For example, something like (and this doesn’t work):

    static ExType TestException<ExType>(string message) where ExType:Exception     {         Exception ex1 = new Exception();         ExType ex = new Exception(message);         return ex;     } 

Now whats confusing me is that we KNOW that the generic type is going to be of an Exception type due to the where clause. However, the code fails because we cannot implicitly cast Exception to ExType. We cannot explicitly convert it either, such as:

    static ExType TestException<ExType>(string message) where ExType:Exception     {         Exception ex1 = new Exception();         ExType ex = (ExType)(new Exception(message));         return ex;     } 

As that fails too.. So is this kind of thing possible? I have a strong feeling its going to be real simple, but I am having a tough day with the old noggin, so cut me some slack 😛


Update

Thanks for the responses guys, looks like it wasn’t me being a complete idiot! 😉

OK, so Vegard and Sam got me on to the point where I could instantiate the correct type, but then obviously got stuck because the message param is read-only following instantiation.

Matt hit the nail right on the head with his response, I have tested this and all works fine. Here is the example code:

    static ExType TestException<ExType>(string message) where ExType:Exception, new ()     {         ExType ex = (ExType)Activator.CreateInstance(typeof(ExType), message);         return ex;     } 

Sweet! 🙂

Thanks guys!

  • 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. 2026-05-10T14:44:22+00:00Added an answer on May 10, 2026 at 2:44 pm

    You can almost do it like this:

    static void TestException<E>(string message) where E : Exception, new() {     var e = new E();     e.Message = message;     throw e; } 

    However, that doesn’t compile because Exception.Message is read only. It can only be assigned by passing it to the constructor, and there’s no way to constrain a generic type with something other than a default constructor.

    I think you’d have to use reflection (Activator.CreateInstance) to ‘new up’ the custom exception type with the message parameter, like this:

    static void TestException<E>(string message) where E : Exception {     throw Activator.CreateInstance(typeof(E), message) as E; } 

    Edit Oops just realised you’re wanting to return the exception, not throw it. The same principle applies, so I’ll leave my answer as-is with the throw statements.

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

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer In neither IE6 nor the standards browsers are the float's… May 11, 2026 at 1:52 pm
  • added an answer class Symbol def to_c self.to_s.camelize.constantize end end :monkey.to_c Updated for… May 11, 2026 at 1:52 pm
  • added an answer I don't think you can update the configuration without changing… May 11, 2026 at 1:52 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.