I experienced on when refactoring some code, maybe I’m blind but I failed to see why the following code won’t work.
public bool Foo()
{
try
{
return SomeFunctionMightThrow();
}
catch (Exception ex)
{
Logger.log(ex);
throw ex;
}
}
So far so good, if I refactor code to
public bool Foo()
{
try
{
return SomeFunctionMightThrow();
}
catch (Exception ex)
{
LogAndThrow(ex);
}
}
private void LogAndThrow(Exception ex)
{
Logger.log(ex);
throw ex;
}
Code does not compile now.
You could potentially change it to…
This essentially does exactly what you want but gives VS the code return path.
Or for a more simplistic approach which will retain the stack trace, just change your catch to: