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

  • Home
  • SEARCH
  • 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 1013889
In Process

The Archive Base Latest Questions

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

As I was working on a project, I thought to myself Hmmm, it would

  • 0

As I was working on a project, I thought to myself “Hmmm, it would be really handy to log a message, and then throw an Exception with that same message”. Since this would let me keep my “exceptions are for exceptional circumstances” principle, but still make sure we’re recording detailed information about what went wrong in the system.

So that gave rise to:

public static class LogAndThrow
{
    public static void Message<TException>(string message) where TException : Exception
    {
        // Log message here

        var constructor = 
            typeof(TException).GetConstructor(new[] { typeof(string) });

        throw (TException)constructor.Invoke(new[] { message });
    }
}

Sure it’s a little rough (and I cut it down for this post), but it works.

However being the type of person to build Exceptions via reflection, I was annoyed that the stack trace would be “tainted” with the LogAndThrow.Message() line.

So I set off to fix that 🙂

I was able to replace the stack trace with some serialization and other trickery, all very stupid, and brute force. But I wanted to figure this out just because.

But I noticed something curious:

var exception = new Exception();
throw exception;

After that exception is created, but before it is thrown, the only thing set is the Message. Stack trace, etc are empty.

The above is equivalent to the following IL:

.locals init (
    [0] class [mscorlib]System.Exception exception)
nop 
newobj instance void [mscorlib]System.Exception::.ctor()
stloc.0 
ldloc.0 
throw 

Which seems to me that the IL for ‘throw’ is doing something more than just taking that reference and walking it up the stack.

Does anyone know what the runtime is doing with the exception on the stack when the IL ‘throw’ is reached?

The trick we used below to change the stack relates to that “magic” in throw I think:

This code is horrible and wrong. More of a science experiment than anything that should ever be put in production evereververEVER

var e = new Exception("message here");
try
{
     throw e;
}
finally
{
    // Get the private file _stackTraceString with reflection
    field.SetValue(e, new StackTrace(1).ToString());
}
  • 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-16T10:10:22+00:00Added an answer on May 16, 2026 at 10:10 am

    Why can’t you modify your static method to return an exception object and throw later. For example

    // Do something
    ...
    // Found error condition, need to throw an exception
    if (error condition)
    {
      throw LogAndThrow.Message("Message goes here");
    }
    

    Edit: AFAIK, there no way to modify stack trace. There are ways to preserve original stack trace while re throwing exceptions – see this article for it.

    Another Edit:

    Just thought that I will put some additional info & links. Basically, CLR builds the stack trace in the exception object only when exception is thrown. This has been mentioned at MSDN – quote from MSDN:

    The common language runtime (CLR) updates the stack trace whenever an
    exception is thrown in application code (by using the throw keyword).
    If the exception was rethrown in a method that is different than the
    method where it was originally thrown, the stack trace contains both
    the location in the method where the exception was originally thrown,
    and the location in the method where the exception was rethrown. If
    the exception is thrown, and later rethrown, in the same method, the
    stack trace only contains the location where the exception was
    rethrown and does not include the location where the exception was
    originally thrown

    This is also mentioned here (where author mentions that CLR will do a stack walk when it encounters the exception in managed code).

    On somewhat related note (but a bit off-topic), see this excellent article (with sample code) where author constructs the alternate stack trace information (basically, he looks up debug information from non-standard location).

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

Sidebar

Related Questions

I myself am convinced that in a project I'm working on signed integers are
I'm currently working on a project that's getting more complex than I thought it
I have a working project that Im amending, it crashes after trying to use
I'm working on a small project for myself at the moment and I'm using
I started a gwt spring 3 project thought spring roo. Then I took roo
I'm working on a C# project that has a post-build event command line that
I have a project i am working on currently and would like to implement
I was working on a project today, and found myself using Math.Max in several
I'm working on a middle-sized project that contains several class libraries, database model and
I'm working on a web project that will (hopefully) be available in several languages

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.