Share via

Two Functions behaves differently

Vuyiswa Maseko 351 Reputation points
2021-01-03T11:01:23.187+00:00

Good Day

i have two functions which are the same but behave differently

public async static Task<TOKEN_MODEL> Retrieve_Token()
        {
            TOKEN_MODEL token = null ;

            try
            {
                try
                {
                    token =  await BlobCache.LocalMachine.GetObject<TOKEN_MODEL>("TOKEN1"); 
                }
                catch (KeyNotFoundException)
                {
                } 
            }
            catch (Exception ex)
            {
                if (ex.Message == "Sequence contains no elements.")
                {
                    return null;
                }
                else
                {
                    return null;
                }
            }
            return token;
        }

And the second Function is

 public async static Task<USERS_Model> RetrieveUserInfo()
        {
             USERS_Model  Userinfo = null; 
            try
            {  
                try
                {  
                   Userinfo   =await BlobCache.LocalMachine.GetObject<USERS_Model>("USERDAT");
                }
                catch (KeyNotFoundException)
                { 
                }   
            }
            catch (KeyNotFoundException ex)
            { 
                if (ex.Message == "Sequence contains no elements.")
                {
                    return null;
                }
                else
                {
                    return null;
                }
            }
            return Userinfo;
        }

and i am calling these two function in App.cs like this

TOKEN_MODEL  token_model = await GenericMethods.Retrieve_Token(); 

and if there is no value , it will return Null and assign the object "token_model " with null which is ok

and the second one which is a problem

USERS_Model userinfo= await GenericMethods.RetrieveUserInfo();

The above line directly jumps to my exception handling code section instead of also assigning the object "userinfo" with null with an exception

System.InvalidOperationException: Sequence contains no elements.

Thanks

Developer technologies | .NET | Xamarin

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.