为什么 SignalR 建议使用 finally 来传播流中的错误?
Why does SignalR recommend using finally to propagate errors in streams?
Wrap logic in a try
... catch
statement. Complete the Channel
in a finally
block. If you want to flow an error, capture it inside the catch
block and write it in the finally
block.
然后他们继续给出一个示例,该示例通过这些卷积没有明显的增益。为什么是这样?从 finally
块中捕获异常并完成通道与在 catch
块中完成通道有什么区别?
可能集中编写器完成逻辑,即使只需要一次调用 - 如果需要,您可能希望在那里插入其他相关逻辑(例如日志记录)。
Exception localException = null;
try
{
// ...
}
catch (Exception ex)
{
localException = ex;
}
finally
{
writer.Complete(localException);
}
对比:
var completed = false;
try
{
// ...
}
catch (Exception ex)
{
writer.Complete(ex);
completed = true;
}
finally
{
if (!completed)
{
writer.Complete(null);
}
}
Wrap logic in a
try
...catch
statement. Complete theChannel
in afinally
block. If you want to flow an error, capture it inside thecatch
block and write it in thefinally
block.
然后他们继续给出一个示例,该示例通过这些卷积没有明显的增益。为什么是这样?从 finally
块中捕获异常并完成通道与在 catch
块中完成通道有什么区别?
可能集中编写器完成逻辑,即使只需要一次调用 - 如果需要,您可能希望在那里插入其他相关逻辑(例如日志记录)。
Exception localException = null;
try
{
// ...
}
catch (Exception ex)
{
localException = ex;
}
finally
{
writer.Complete(localException);
}
对比:
var completed = false;
try
{
// ...
}
catch (Exception ex)
{
writer.Complete(ex);
completed = true;
}
finally
{
if (!completed)
{
writer.Complete(null);
}
}