Click here to Skip to main content
15,844,904 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# Get random object form JSON File Pin
NotPolitcallyCorrect4-Jan-17 8:41
NotPolitcallyCorrect4-Jan-17 8:41 
GeneralRe: C# Get random object form JSON File Pin
Gerry Schmitz4-Jan-17 8:59
mveGerry Schmitz4-Jan-17 8:59 
GeneralRe: C# Get random object form JSON File Pin
NotPolitcallyCorrect4-Jan-17 9:32
NotPolitcallyCorrect4-Jan-17 9:32 
GeneralRe: C# Get random object form JSON File Pin
OriginalGriff4-Jan-17 9:11
mveOriginalGriff4-Jan-17 9:11 
GeneralRe: C# Get random object form JSON File Pin
NotPolitcallyCorrect4-Jan-17 9:27
NotPolitcallyCorrect4-Jan-17 9:27 
QuestionA Better Way? Pin
Kevin Marois4-Jan-17 6:35
professionalKevin Marois4-Jan-17 6:35 
AnswerRe: A Better Way? Pin
Eddy Vluggen4-Jan-17 8:06
professionalEddy Vluggen4-Jan-17 8:06 
AnswerRe: A Better Way? Pin
Jon McKee4-Jan-17 11:00
professionalJon McKee4-Jan-17 11:00 
Since Task.Exception is an AggregateException you've gotta worry about multiple inner exceptions in addition to the current exception the AggregateException represents unless you know precisely how many exceptions can be thrown. My ideas:

Log current exception, flatten (in the case of nested AggregateExceptions), and re-throw:
_hubProxy.Invoke("SomeMethodName", model).ContinueWith((t) =>
{
    if (t.Exception != null && t.Exception.InnerException != null)
    {
        //Log the current exception
        throw t.Exception.Flatten();
    }
});

Handle exceptions you know how to handle, re-throw the rest:
_hubProxy.Invoke("SomeMethodName", model).ContinueWith((t) =>
{   
    if (t.Exception != null && t.Exception.InnerException != null)
    {
        AggregateException ae = t.Exception.Flatten();
        ae.Handle(x =>
        {
              if (x is IndexOutOfRangeException)
              {
                //Do stuff
                return true;
              }
              return false;
        });
    }
});

You can avoid Flatten() if you're absolutely sure there are no tasks inside your task. Also the reason I used Handle() is because it conveniently re-wraps all exceptions that did not return true (not handled) in an AggregateException and re-throws them. If you can handle everything or are sure of exactly what exceptions can be thrown you could iterate over the AggregateException.InnerExceptions collection instead and re-throw as you desire.

EDIT: Quick addendum: If you're using C#6 you can simply the if-statement to:
C#
if (t.Exception?.InnerException != null)
Looks cleaner to me but to each their own Thumbs Up | :thumbsup:

modified 4-Jan-17 21:37pm.

AnswerRe: A Better Way? Pin
Pete O'Hanlon4-Jan-17 21:46
subeditorPete O'Hanlon4-Jan-17 21:46 
GeneralRe: A Better Way? Pin
Kevin Marois5-Jan-17 5:32
professionalKevin Marois5-Jan-17 5:32 
GeneralRe: A Better Way? Pin
Eddy Vluggen5-Jan-17 5:36
professionalEddy Vluggen5-Jan-17 5:36 
GeneralRe: A Better Way? Pin
Pete O'Hanlon5-Jan-17 7:00
subeditorPete O'Hanlon5-Jan-17 7:00 
AnswerRe: A Better Way? Pin
#realJSOP6-Jan-17 5:45
mve#realJSOP6-Jan-17 5:45 
GeneralRe: A Better Way? Pin
Kevin Marois6-Jan-17 5:47
professionalKevin Marois6-Jan-17 5:47 
QuestionC#.net:Listen to event fire on app minimize to tray Pin
hassaan mustafa4-Jan-17 4:07
hassaan mustafa4-Jan-17 4:07 
AnswerRe: C#.net:Listen to event fire on app minimize to tray Pin
Eddy Vluggen4-Jan-17 8:05
professionalEddy Vluggen4-Jan-17 8:05 
QuestionInteracting with a Page Web Service (OData v4) Error Pin
MaWeRic3-Jan-17 23:03
MaWeRic3-Jan-17 23:03 
AnswerRe: Interacting with a Page Web Service (OData v4) Error Pin
Gerry Schmitz4-Jan-17 10:23
mveGerry Schmitz4-Jan-17 10:23 
GeneralRe: Interacting with a Page Web Service (OData v4) Error Pin
MaWeRic4-Jan-17 10:27
MaWeRic4-Jan-17 10:27 
GeneralRe: Interacting with a Page Web Service (OData v4) Error Pin
Gerry Schmitz4-Jan-17 10:31
mveGerry Schmitz4-Jan-17 10:31 
GeneralRe: Interacting with a Page Web Service (OData v4) Error Pin
MaWeRic4-Jan-17 12:01
MaWeRic4-Jan-17 12:01 
GeneralRe: Interacting with a Page Web Service (OData v4) Error Pin
MaWeRic4-Jan-17 12:43
MaWeRic4-Jan-17 12:43 
GeneralRe: Interacting with a Page Web Service (OData v4) Error Pin
Gerry Schmitz4-Jan-17 13:31
mveGerry Schmitz4-Jan-17 13:31 
GeneralRe: Interacting with a Page Web Service (OData v4) Error Pin
MaWeRic4-Jan-17 20:13
MaWeRic4-Jan-17 20:13 
GeneralRe: Interacting with a Page Web Service (OData v4) Error Pin
Gerry Schmitz4-Jan-17 20:47
mveGerry Schmitz4-Jan-17 20:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.