通过等待任务或访问其异常属性,没有观察到任务的异常 [英] A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property

查看:955
本文介绍了通过等待任务或访问其异常属性,没有观察到任务的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是我的任务。我应该如何修改它们以防止此错误。我检查了其他类似的线程,但我正在等待并继续。那么这个错误是怎么发生的?


任务的异常没有被等待任务或访问其异常属性所观察到。因此,未知的异常被终结者线程重新抛出。




  var CrawlPage = Task .Factory.StartNew(()=> 
{
return crawlPage(srNewCrawledUrl,srNewCrawledPageId,srMainSiteId);
});

var GetLinks = CrawlPage.ContinueWith(resultTask =>
{
if(CrawlPage.Result == null)
{
return null;
}
else
{
return ReturnLinks(CrawlPage.Result,srNewCrawledUrl,srNewCrawledPageId,srMainSiteId);
}
});

var InsertMainLinks = GetLinks.ContinueWith(resultTask =>
{
if(GetLinks.Result == null)
{

}
else
{
instertLinksDatabase(srMainSiteURL,srMainSiteId,GetLinks.Result,srNewCrawledPageId,irCrawlDepth.ToString());
}

});

InsertMainLinks.Wait();
InsertMainLinks.Dispose();


解决方案

您没有处理任何异常。 >

更改此行:

  InsertMainLinks.Wait(); 

TO:

 code> try {
InsertMainLinks.Wait();
}
catch(AggregateException ae){
/ *你会做什么* /
}

一般来说:为了防止终结者重新抛出您工作线程中发生的任何未处理的异常,您可以:



等待线程并捕获System.AggregateException,或者只是读取异常属性。



EG:

  Task.Factory.StartNew((s)=> {
throw new Exception(ooga booga);
},TaskCreationOptions.None).ContinueWith((Task上一个)=> {
var e = previous.Exception;
//做你会用非空异常
});

  Task.Factory.StartNew((s)=> {
throw new Exception(ooga booga);
},TaskCreationOptions.None).ContinueWith((以前的任务)=> ; {
try {
previous.Wait();
}
catch(System.AggregateException ae){
// do you you
}
});


These are my tasks. How should i modify them to prevent this error. I checked the other similar threads but i am using wait and continue with. So how come this error happening?

A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread.

    var CrawlPage = Task.Factory.StartNew(() =>
{
    return crawlPage(srNewCrawledUrl, srNewCrawledPageId, srMainSiteId);
});

var GetLinks = CrawlPage.ContinueWith(resultTask =>
{
    if (CrawlPage.Result == null)
    {
        return null;
    }
    else
    {
        return ReturnLinks(CrawlPage.Result, srNewCrawledUrl, srNewCrawledPageId, srMainSiteId);
    }
});

var InsertMainLinks = GetLinks.ContinueWith(resultTask =>
{
    if (GetLinks.Result == null)
    {

    }
    else
    {
        instertLinksDatabase(srMainSiteURL, srMainSiteId, GetLinks.Result, srNewCrawledPageId, irCrawlDepth.ToString());
    }

});

InsertMainLinks.Wait();
InsertMainLinks.Dispose();

解决方案

You're not handling any exception.

Change this line:

InsertMainLinks.Wait();

TO:

try { 
    InsertMainLinks.Wait(); 
}
catch (AggregateException ae) { 
    /* Do what you will */ 
}

In general: to prevent the finalizer from re-throwing any unhandled exceptions originating in your worker thread, you can either:

Wait on the thread and catch System.AggregateException, or just read the exception property.

EG:

Task.Factory.StartNew((s) => {      
    throw new Exception("ooga booga");  
}, TaskCreationOptions.None).ContinueWith((Task previous) => {  
    var e=previous.Exception;
    // Do what you will with non-null exception
});

OR

Task.Factory.StartNew((s) => {      
    throw new Exception("ooga booga");  
}, TaskCreationOptions.None).ContinueWith((Task previous) => {      
    try {
        previous.Wait();
    }
    catch (System.AggregateException ae) {
        // Do what you will
    }
});

这篇关于通过等待任务或访问其异常属性,没有观察到任务的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆