【发布时间】:2011-03-20 11:04:37
【问题描述】:
当使用 BeginXXX / EndXXX 模式使用异步代码从流等中读取时,我相信在调用 EndXXX 时会抛出该过程中发生的任何异常。
这是否意味着对 BeginXXX 的初始调用永远不会抛出异常,它总是会被 EndXXX 抛出?
或者换一种说法,我是否也应该在 BeginRead 后面加上 try{}catch{}?
public StartReading()
{
// Should this be enclosed with try{}catch{} ?
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(readCallback), stream);
}
private void readCallback(IAsyncResult result)
{
Stream stream = (Stream)result.AsyncState;
try
{
int len = stream.EndRead(result);
// Do work...
}
catch(Exception ex)
{
// Error handling stuff.
}
}
【问题讨论】:
标签: c# winforms exception-handling asynchronous