private static void Foo(Exception e)
{
e = new Exception("msg1");
}
static void Main(string[] args)
{
try
{
int zero = 0;
int ecks = 1 / zero;
}
catch (Exception e)
{
// I thought Exception is passed by reference, therefore Foo changes e to
// point to new Exception instance with message "msg1".
// But it stays the same
Foo(e);
throw e;
}
}
It works for classes e.g.
public class MyClass
{
public string Name { get; set; }
}
private static void Foo(MyClass m) { m.Name = "bar"; }
static void Main(string[] args)
{
Voda v = new Voda();
v.Name = "name";
Foo(v); // v.Name gets value "bar"
}
According to msdn Exception is class.
EDIT
private static void Foo(Exception e)
{
while (e != null && e.InnerException != null)
{
// I'm changing where e points.
// Therefore caller Exception should now point to most inner exception
e = e.InnerException;
});
}