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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:29:41+00:00 2026-05-12T23:29:41+00:00

Consider this method (pardon the sad attempt at Chuck Norris humor :) ): public

  • 0

Consider this method (pardon the sad attempt at Chuck Norris humor 🙂 ):

public class ChuckNorrisException : Exception
{
    public ChuckNorrisException()
    {
    }

    public ChuckNorrisException(string message)
        : base(message)
    {
    }

    public ChuckNorrisException(string message, Exception cause)
        : base(message, cause)
    {
    }

    protected ChuckNorrisException(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
    }
}

static void ExceptionTest(double x)
{
    try
    {
        double y = 10 / x;
        Console.WriteLine("quotient = " + y);
    }
    catch (Exception e)
    {
        e = e is DivideByZeroException ? new ChuckNorrisException("Only Chuck Norris can divide by 0!", e) :
            e;
        throw e;
    }
}

In resharper, I get a warning on the “throw e” line saying “Exception rethrow possibly intended”. But obviously in this case that’s not the intention, since e could be wrapped in ChuckNorrisException, and if I just use “throw”, that wrapped exception wouldn’t get thrown.

I know I can suppress the resharper warning, but then it will be turned off for all scenarios if I’m not mistaken. I just wondered if anybody else had encountered this. The only workaround I’ve found is to make another exception variable (e2, for example), and throw that. That may be the best I can do here. Seems like resharper could detect this issue though and be smart enough to know that if e is modified, then throw e is ok.

Thanks.

[EDIT]
Sorry, I forgot a step. Before the throw, I need to log the exception, so I can’t just do:

e = e is DivideByZeroException ? new ChuckNorrisException("Only Chuck Norris can divide by 0!", e) :
            e;
throw e;

I have to do:

e = e is DivideByZeroException ? new ChuckNorrisException("Only Chuck Norris can divide by 0!", e) :
            e;
LogException(e);
throw e;
  • 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-12T23:29:41+00:00Added an answer on May 12, 2026 at 11:29 pm

    Maybe I’m not understanding the question, so please correct me if I’ve got the wrong end of the stick.

    There’s two cases going on here:

    1. The first is that you catch the
      original exception. You then wrap
      it in a new exception instance as
      the inner exception, then throw the
      new one. No information is lost in this case (the inner exception preserves all information), so no warning is given.

    2. The second is that you catch and
      re-throw the original exception. If you re-throw, you
      should never use “throw e”, as it
      will tamper with the stack trace.
      This is why ReSharper is printing a
      warning. To re-throw the caught exception, you should use the “throw” keyword on its own.

    The answer to this question explains it better than I can. Due to the subtle side effects and the sheer number of people who get this detail wrong, I personally view the re-throw syntax as flawed.

    Anyway that’s a description of why you’re getting a warning. Here’s what I’d do about it instead:

    catch(DivideByZeroException e)
    {
        // we don't catch any other exceptions because we weren't
        // logging them or doing anything with the exception before re-throwing
        throw new ChuckNorrisException("Useful information", e);
    }
    

    *Edit — if you need to log exceptions, you can just do something like this instead. Note: This is my preferred solution as I think it reads better and is less likely to contain an error than querying for exception types yourself:

    // catch most specific exception type first
    catch(DivideByZeroException e)
    {
        Log(e); 
        throw new ChuckNorrisException("Useful information", e);
    } 
    catch(Exception e) // log & re-throw all other exceptions
    {
        Log(e);
        throw; // notice this is *not* "throw e"; this preserves the stack trace
    }
    

    Another alternative would be:

    catch(Exception e)
    {
        Log(e);
        if(e is DivideByZeroException)
        {
            // wrap + throw the exception with our new exception type
            throw new ChuckNorrisException("useful info", e);
        }
    
        // re-throw the original, preserving the stack trace
        throw;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this: public class TestClass { private String a; private String b; public TestClass()
Consider this method signature: public static void WriteLine(string input, params object[] myObjects) { //
Consider this class. public class DynamicField implements Comparable<DynamicField> { String title; int position; int
Consider type like this one public interface IHaveGenericMethod { T1 Method<T1>(T1 parm); T2 Method<T1,T2>(T1
Consider this Java code class A{ //@returns Class object this method is contained in
This bug took me a while to find... Consider this method: public void foo(Set<Object>
Consider this example The Interface interface IBusinessRules { string Perform(); } The Inheritors class
Please pardon me for a n00bish question. Please consider the following code: public class
Consider this method: public IEnumerable<T> GetList(int Count) { foreach (var X in Y) {
Consider this code : class App { public static function log($msg) { echo $msg;

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.