I am fully aware that what I'm going to ask is not good practice ... but:
Let's say I have a class containing a function in which I want to always return a value, but keep any exceptions that may arise for later processing. Sort of:
public Exception _error { get; set; } public bool IsValid() { try {
Now that I have saved the exception, is it possible to exclude the exception from the external method altogether, preserving both the original stack trace and the type of exception?
throw _error; //lose stack trace throw new Exception("", _error) //lose type
Thanks for watching or responding.
EDIT:
Thanks to some additional points, I understand that the idea below only takes information and does not actually add or simplify the situation. Thanks again to everyone.
After thinking about Peter's answers and comments, I now wonder if the Exception class can make a partial solution as shown below. This overrides as many exceptions as possible so that the New exception looks like its innerexception, including stacktrace .. dirty I know, but interesting:
public class ExceptionWrapper : Exception { private Exception _innerException; public ExceptionWrapper(Exception ex) : base("", ex) { _innerException = ex; this.Source = ex.Source; this.HelpLink = ex.HelpLink; } public override string StackTrace { get { return _innerException.StackTrace; } } public override System.Collections.IDictionary Data { get { return _innerException.Data; } } public override string Message { get { return _innerException.Message; } } public new Exception InnerException { get { return _innerException.InnerException; } } }
Strike>
source share