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
.