Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have got the following set of code

C#
Task<List<ExchangeRateDto>> getUsdExchangeRatesTask =
                    Task.Factory.StartNew(() => _accountsService.GetExchangeRates("USA", "USD"));
                
                JobDetailsModel model = GetJobDetailsModel(getUsdExchangeRatesTask.Result, job);



In the above, if the call to the _accountService fails due to some reason like service down or the endpoint does not match then the "
getUsdExchangeRatesTask.Result
" throws an exception.

I would prefer to handle in the way that the call to the service fails then
getUsdExchangeRatesTask.Result
should be assigned as Null so that the further call to other methods doesn't affect.

What I have tried:

I tried using try catch block but it just catches the exception but I would like to assign NUll to the value. Can someone please help me out to fix this issue.
Posted
Updated 25-Jan-19 1:40am
v2
Comments
MadMyche 25-Jan-19 6:38am    
Can you show us the try....catch version?
Richard Deeming 25-Jan-19 6:53am    
Why are you starting a new thread to call a synchronous method if you're then immediately blocking the current thread to wait for that method to complete? Just call the method directly.

1 solution

In your catch block you need to check which exception has been thrown. You can then decide whether you can just set the result to null, or need to take some further action.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900