Preservação do StackTrace / LineNumbers original nas exceções do .NET
Entendendo a diferença entrejogar ex elançar, por que o StackTrace original é preservado neste exemplo:
static void Main(string[] args)
{
try
{
LongFaultyMethod();
}
catch (System.Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
static void LongFaultyMethod()
{
try
{
int x = 20;
SomethingThatThrowsException(x);
}
catch (Exception)
{
throw;
}
}
static void SomethingThatThrowsException(int x)
{
int y = x / (x - x);
}
Mas não neste:
static void Main(string[] args)
{
try
{
LongFaultyMethod();
}
catch (System.Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
static void LongFaultyMethod()
{
try
{
int x = 20;
int y = x / (x - 20);
}
catch (Exception)
{
throw;
}
}
O segundo cenário está produzindo a mesma saídajogar ex seria?
Em ambos os casos, espera-se ver o número da linha em que y é inicializado.