InnerException

noun

say it: “IN-er ek-SEP-shun” /ˈɪnər ɪkˈsɛpʃən/

A property of a C# exception that holds the exception that caused it, kept when a method catches one exception and throws a clearer one.

What is InnerException in C#?

InnerException — A property of every exception that holds the exception that caused it, or null. When a method catches an exception and throws a new one with a clearer message, it passes the original in, so nothing is lost.

try
{
    decimal.Parse("n/a");
}
catch (FormatException ex)
{
    FormatException clearer = new FormatException("Line 2: can't read 'David,n/a'", ex);
    Console.WriteLine(clearer.InnerException?.Message); // The input string 'n/a' was not in a correct format.
}

In a sentence

  • “The new exception says which line was bad, and its InnerException still holds the original FormatException from decimal.Parse.”

Level: Journeyman · More C# terms at this level

Updated