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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:52:26+00:00 2026-05-21T18:52:26+00:00

As all of you probably know, catching and re-throwing a exception in c# this

  • 0

As all of you probably know, catching and re-throwing a exception in c# this way is evil, because it destroys the stack trace:

try
{
    if(dummy)
        throw new DummyException();
}
catch (DummyException ex)
{
    throw ex;
}

The right way to re-throw an exception without loosing the stack trace is this:

try
{
    if(dummy)
        throw new DummyException();
}
catch (DummyException ex)
{
    throw;
}

The only problem with this is that I get a lot of compilation warnings: “The variable ‘ex’ is declared but never used”. If you have a lot of these, a useful warning may be hidden in the garbage. So, that’s what I did:

try
{
    if(dummy)
        throw new DummyException();
}
catch (DummyException)
{
    throw;
}
catch(AnotherException ex)
{
    //handle it
}

This seems to work, but I’d like to know if there is any downside of re-throwing an exception that is not set to an variable. How does .net threats this?

Thanks in advance

Edit:
I’ve changed my code a little bit to make clearer what I wanted to do, as some had misunderstood

  • 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-21T18:52:27+00:00Added an answer on May 21, 2026 at 6:52 pm

    I’d like to know if there is any downside of re-throwing an exception that is not set to an variable.

    No, there’s no downside at all. A variable is only needed if you want to reference the exception in your code, but since you don’t need to do that with a throw statement, you don’t need a variable at all.

    And you have exactly the right idea in attempting to eliminate “noisy” compiler warnings. They have a tendency to bury important errors that you do want to fix, and getting a clean build is always important. The best solution is simply to rewrite the code to use a parameterless catch clause.

    However, be aware that in 82% of cases that I see*, it’s a mistake to write code that uses throw at all. You generally shouldn’t catch exceptions that you don’t know how to handle and your only intended “handling” strategy is to rethrow them. There are cases where even using throw can reset the call stack, causing you to lose important debugging information. There are also better alternatives for logging exceptions to catching/rethrowing. You can find more information in the answers to these questions:

    • Main method code entirely inside try/catch: Is it bad practice?
    • what can lead throw to reset a callstack (I'm using "throw", not "throw ex")

    There’s absolutely nothing wrong with letting exceptions bubble up and handling them all in a central place. The rule to keep in mind is that you shouldn’t use exceptions for flow control. But there’s nothing wrong with throwing an exception in low level code, and showing the user an error message higher up the stack in UI code. Read Microsoft’s Best Practices for Handling Exceptions for some general tips.


    * Slightly more than the percent of statistics that are made up on the spot.

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

Sidebar

Related Questions

first of all i would like to say i know its probably an easy
First of all there is probably a question like this already but i couldn't
Morning y'all This is probably an easy one but I barely got any sleep
I have code that all over the place(probably 20-30 instances) does this: <widget>.setVisible((condition ==
I've been working all day and I somehow can't get this probably easy task
I am using ASP.Net MVC but this probably applies to all MVC patterns in
This is probably pretty simple. I want to select all elements of a given
Sorry this is probably super basic. But in all my javabean examples, I've not
So as we all probably know, the atoi converts a char to a number.
You probably all know the famous virtual zippo app. I thought of making such

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.