A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property

11,494

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
    }
});
Share:
11,494
MonsterMMORPG
Author by

MonsterMMORPG

Hello. I am the only owner and developer of web based online MMORPG game MonsterMMORPG. I am a computer engineer from Turkey and i am currently doing MA at computer engineering. I am specialized with C# & ASP.net. http://www.monstermmorpg.com/ MonsterMMORPG is a Free To Play Browser Based Online Monster MMORPG Game Better Than Online Pokemon Games You will love it's awesome Monsters We have many different unique features. So i suggest you to check them out. Our game is similiar with Pokemon games but it is unique. Like diablo and torch light. You should visit following sites related to us MonsterMMORPG Facebook Pokemon Games Lovers Facebook Application MonsterMMORPG Youtube Channel Monster Game Forum Canavar Oyunu Forum Pokemon Fakemon DeviantArt MonsterMMORPG Google Plus MonsterMMORPG Twitter MonsterMMORPG Review On Browsergamez MonsterMMORPG Review On mmohuts MonsterMMORPG Developer Blog At MMORPG.com MonsterMMORPG Review On onrpg MonsterMMORPG On GameSpot MonsterMMORPG Wiki MonsterMMORPG On 1UP MonsterMMORPG Digg MonsterMMORPG Official Google Plus Page

Updated on June 04, 2022

Comments

  • MonsterMMORPG
    MonsterMMORPG almost 2 years

    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();