Click here to Skip to main content
15,895,557 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# Get random object form JSON File Pin
NotPolitcallyCorrect4-Jan-17 7:41
NotPolitcallyCorrect4-Jan-17 7:41 
GeneralRe: C# Get random object form JSON File Pin
Gerry Schmitz4-Jan-17 7:59
mveGerry Schmitz4-Jan-17 7:59 
GeneralRe: C# Get random object form JSON File Pin
NotPolitcallyCorrect4-Jan-17 8:32
NotPolitcallyCorrect4-Jan-17 8:32 
GeneralRe: C# Get random object form JSON File Pin
OriginalGriff4-Jan-17 8:11
mveOriginalGriff4-Jan-17 8:11 
GeneralRe: C# Get random object form JSON File Pin
NotPolitcallyCorrect4-Jan-17 8:27
NotPolitcallyCorrect4-Jan-17 8:27 
QuestionA Better Way? Pin
Kevin Marois4-Jan-17 5:35
professionalKevin Marois4-Jan-17 5:35 
AnswerRe: A Better Way? Pin
Eddy Vluggen4-Jan-17 7:06
professionalEddy Vluggen4-Jan-17 7:06 
AnswerRe: A Better Way? Pin
Jon McKee4-Jan-17 10:00
professionalJon McKee4-Jan-17 10: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 20:46
mvePete O'Hanlon4-Jan-17 20:46 
GeneralRe: A Better Way? Pin
Kevin Marois5-Jan-17 4:32
professionalKevin Marois5-Jan-17 4:32 
GeneralRe: A Better Way? Pin
Eddy Vluggen5-Jan-17 4:36
professionalEddy Vluggen5-Jan-17 4:36 
GeneralRe: A Better Way? Pin
Pete O'Hanlon5-Jan-17 6:00
mvePete O'Hanlon5-Jan-17 6:00 
AnswerRe: A Better Way? Pin
#realJSOP6-Jan-17 4:45
mve#realJSOP6-Jan-17 4:45 
GeneralRe: A Better Way? Pin
Kevin Marois6-Jan-17 4:47
professionalKevin Marois6-Jan-17 4:47 
QuestionC#.net:Listen to event fire on app minimize to tray Pin
hassaan mustafa4-Jan-17 3:07
hassaan mustafa4-Jan-17 3:07 
AnswerRe: C#.net:Listen to event fire on app minimize to tray Pin
Eddy Vluggen4-Jan-17 7:05
professionalEddy Vluggen4-Jan-17 7:05 
QuestionInteracting with a Page Web Service (OData v4) Error Pin
MaWeRic3-Jan-17 22:03
MaWeRic3-Jan-17 22:03 
AnswerRe: Interacting with a Page Web Service (OData v4) Error Pin
Gerry Schmitz4-Jan-17 9:23
mveGerry Schmitz4-Jan-17 9:23 
GeneralRe: Interacting with a Page Web Service (OData v4) Error Pin
MaWeRic4-Jan-17 9:27
MaWeRic4-Jan-17 9:27 
GeneralRe: Interacting with a Page Web Service (OData v4) Error Pin
Gerry Schmitz4-Jan-17 9:31
mveGerry Schmitz4-Jan-17 9:31 
GeneralRe: Interacting with a Page Web Service (OData v4) Error Pin
MaWeRic4-Jan-17 11:01
MaWeRic4-Jan-17 11:01 
GeneralRe: Interacting with a Page Web Service (OData v4) Error Pin
MaWeRic4-Jan-17 11:43
MaWeRic4-Jan-17 11:43 
GeneralRe: Interacting with a Page Web Service (OData v4) Error Pin
Gerry Schmitz4-Jan-17 12:31
mveGerry Schmitz4-Jan-17 12:31 
GeneralRe: Interacting with a Page Web Service (OData v4) Error Pin
MaWeRic4-Jan-17 19:13
MaWeRic4-Jan-17 19:13 
GeneralRe: Interacting with a Page Web Service (OData v4) Error Pin
Gerry Schmitz4-Jan-17 19:47
mveGerry Schmitz4-Jan-17 19: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.