c# - Catch Exception and rethrow with a new message preserving actual type -


i have catch (exception ex), change message, , rethrow it.

if throw new exception(newmessage, ex);, lose runtime type of exception, right?

if throw;, can keep runtime type message doesn't change.

since i'm catching, don't know derived type ex may be, can't construct proper type.

and of course, if throw ex; lose stack trace.

is there way reformat message of ex , rethrow without losing stack or runtime type information, without resorting weird brittle reflection hacks?

since have eliminated reasonable solutions problem, answer remains "no, can't this". message of exception read-only. cannot set using "brittle reflection hacks", since property virtual , therefore not required come backing field @ all, particular exception type. following class should demonstrate impossibility of demand:

sealed class ozymandiasexception : exception {     public override string message => "look upon message, ye mighty, , despair."; } 

there no way produce instance of ozymandiasexception when caught have different message.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo