Found something yesterday that made me realise that I'm most likely still missing fundamental tidbits about C#.
I have a Stateless Service Fabric Application. I have a try-catch around the main while-loop. If I throw an exception in this loop, it breaks out of the while-loop and the service effectively stops. If I add a throw
to the catch clause, the service restarts.
protected override async Task RunAsync(CancellationToken cancellationToken)
{
try
{
long iterations = 0;
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
await Task.Delay(TimeSpan.FromSeconds(3), cancellationToken).ConfigureAwait(false);
// Do stuff...
if (iterations > 4) {
throw new Exception();
}
ServiceEventSource.Current.ServiceMessage(this.Context, $"Working-{++iterations}");
}
}
catch (Exception ex)
{
// Log stuff...
throw;
}
}
Can someone explain why this is or direct me to where I can get an answer? I couldn't find anything that explain this behaviour.
EDIT: It's not a duplicate of Is there a difference between "throw" and "throw ex"? because, as far as I can see, there it no explanation of my question, why the function is being run again. That topic is more about explaining the difference between throw
and throw new
respective of the stack trace.