Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Exception stack trace ever be null?

Tags:

c#

exception

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?
}
like image 250
Henley Avatar asked Feb 10 '14 21:02

Henley


People also ask

Can an exception have a null message?

Yes, the message is only optional. But I think it's not a very good software design to let the message null, because the message helps the user to get more detailed information about the error.

What is an exception stack trace?

A trace of the method calls is called a stack trace. The stack trace listing provides a way to follow the call stack to the line number in the method where the exception occurs. The StackTrace property returns the frames of the call stack that originate at the location where the exception was thrown.

Does exception message contain stack trace?

The stack trace contains the Exception's type and a message, and a list of all the method calls which were in progress when it was thrown.

What is exception stack trace in Java?

A Java stack trace is displayed when an error or exception occurs. The stack trace, also called a backtrace, consists of a collection of stack records, which store an application's movement during its execution.


2 Answers

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; }
    }
}
like image 178
Preston Guillot Avatar answered Oct 09 '22 23:10

Preston Guillot


Yes.

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

like image 36
SLaks Avatar answered Oct 09 '22 22:10

SLaks