Can Exception stack trace ever be null?

2020-04-02 07:19发布

问题:

I found out that if I am catching an Exception e, e.innerException could possibly be null.

Is it also possible that e.StackTrace could also be null in any possible circumstance in a catch block?

try {

}
catch(Exception e)
{
//can e.StackTrace be null here?
}

回答1:

Yes.

If you create a new Exception() and don't throw it, every property except Data and Message will be null.



回答2:

An example that proves the StackTrace can be null in a catch block is equally trivial to show:

public class DerpException : Exception
{
    public override string StackTrace
    {
        get { return null; }
    }
}


标签: c# exception