C# 异常在哪里捕获?(试试看)
当使用后台工作类在try-catch语句中调用一个方法,并且该方法中存在try-catch语句时,哪个类捕获异常C# 异常在哪里捕获?(试试看),c#,exception,try-catch,C#,Exception,Try Catch,当使用后台工作类在try-catch语句中调用一个方法,并且该方法中存在try-catch语句时,哪个类捕获异常 private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { try { Do(); } catch (Exception ex) { System.Windows.MessageBo
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
try
{
Do();
}
catch (Exception ex)
{
System.Windows.MessageBox.Show("Error:" + e.Result + ex.Message);
}
}
以及:
调用时使用:backgroundWorker1.RunWorkerAsync()代码>
是否有方法确保在方法中处理异常?因此后台工作人员不会破坏内部工作人员,因为此捕获“更接近”错误
这个:
private void Do ()
{
try
{
//Do something, open a file etc.
FileStream fs = new FileStream("file.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
catch (Exception e)
{
System.Windows.MessageBox.Show("Error:" + e.Result + ex.Message);
}
}
哪一个例外?“因此backgroundworker不会中断”-表示您在上面的代码中有问题,哪些问题?如果在dowwork
函数中未处理异常,则它将作为参数的一部分传递给RunWorkerCompleted
函数。请参阅。输入一个断点,或向日志中添加一些内容,以查看是哪一个正在写入断点。任何一个都只需要几秒钟的时间来检查。不,不。当backgroundworker类在VS2015中中断时,它不会显示它在哪里中断。因此我的问题是
private void Do ()
{
try
{
//Do something, open a file etc.
FileStream fs = new FileStream("file.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
catch (Exception e)
{
System.Windows.MessageBox.Show("Error:" + e.Result + ex.Message);
}
}