Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Proper Error handling

Proper Error handling

Scheduled Pinned Locked Moved C#
csharpandroidhelpdiscussion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Exoskeletor
    wrote on last edited by
    #1

    greetings guys. im building an android application and i have some second thoughts on some error handling case. I have a method that gets data from the internet by calling this method:

    public static string StoredDatesList
    {
    get => Preferences.Get(nameof(StoredDatesList), string.Empty);
    set => Preferences.Set(nameof(StoredDatesList), value);
    }
    public static async Task GetDraws(Uri url, string date)
    {
    Dictionary StoredDates = new Dictionary();
    StoredDates = JsonConvert.DeserializeObject>(StoredDatesList);
    var contents = string.Empty;
    HttpClient client = new HttpClient();

            if (StoredDates != null)
                if (StoredDates.ContainsKey(date))
                {
                    contents = StoredDates\[date\];
                }
                else
                {
                    var current = Connectivity.NetworkAccess;
                    if (current != NetworkAccess.Internet)
                        return null;
                    client = new HttpClient();
                    contents = await client.GetStringAsync(url);
                    var res2 = JsonConvert.DeserializeObject(contents.ToString());
                    if (180 == res2.content.Count)
                    {
                        StoredDates.Add(date, contents);
                        StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented);
                    }
                }
            else
            {
                StoredDates = new Dictionary();
    
                contents = await client.GetStringAsync(url);
                var res2 = JsonConvert.DeserializeObject(contents.ToString());
                if (180 == res2.content.Count)
                {
                    StoredDates.Add(date, contents);
                    StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented);
                }
            }
            return contents;
        }
    

    the if statement

    current != NetworkAccess.Internet)

    checks if internet is available, when internet is not available i return null and i check if the data is null and im displaying a message(error, internet is not available etc). I find this approach very bad and im trying to think how is the proper way to handle this. i cannot show a message t

    L G 2 Replies Last reply
    0
    • E Exoskeletor

      greetings guys. im building an android application and i have some second thoughts on some error handling case. I have a method that gets data from the internet by calling this method:

      public static string StoredDatesList
      {
      get => Preferences.Get(nameof(StoredDatesList), string.Empty);
      set => Preferences.Set(nameof(StoredDatesList), value);
      }
      public static async Task GetDraws(Uri url, string date)
      {
      Dictionary StoredDates = new Dictionary();
      StoredDates = JsonConvert.DeserializeObject>(StoredDatesList);
      var contents = string.Empty;
      HttpClient client = new HttpClient();

              if (StoredDates != null)
                  if (StoredDates.ContainsKey(date))
                  {
                      contents = StoredDates\[date\];
                  }
                  else
                  {
                      var current = Connectivity.NetworkAccess;
                      if (current != NetworkAccess.Internet)
                          return null;
                      client = new HttpClient();
                      contents = await client.GetStringAsync(url);
                      var res2 = JsonConvert.DeserializeObject(contents.ToString());
                      if (180 == res2.content.Count)
                      {
                          StoredDates.Add(date, contents);
                          StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented);
                      }
                  }
              else
              {
                  StoredDates = new Dictionary();
      
                  contents = await client.GetStringAsync(url);
                  var res2 = JsonConvert.DeserializeObject(contents.ToString());
                  if (180 == res2.content.Count)
                  {
                      StoredDates.Add(date, contents);
                      StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented);
                  }
              }
              return contents;
          }
      

      the if statement

      current != NetworkAccess.Internet)

      checks if internet is available, when internet is not available i return null and i check if the data is null and im displaying a message(error, internet is not available etc). I find this approach very bad and im trying to think how is the proper way to handle this. i cannot show a message t

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      If your app can continue without the data, and the user can do nothing about it, there is no point in telling them. It's called: graceful degradation.

      It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

      E 1 Reply Last reply
      0
      • L Lost User

        If your app can continue without the data, and the user can do nothing about it, there is no point in telling them. It's called: graceful degradation.

        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

        E Offline
        E Offline
        Exoskeletor
        wrote on last edited by
        #3

        No, i meant that not all the times data is needed. When it does, it will throw an error if you dont have

        1 Reply Last reply
        0
        • E Exoskeletor

          greetings guys. im building an android application and i have some second thoughts on some error handling case. I have a method that gets data from the internet by calling this method:

          public static string StoredDatesList
          {
          get => Preferences.Get(nameof(StoredDatesList), string.Empty);
          set => Preferences.Set(nameof(StoredDatesList), value);
          }
          public static async Task GetDraws(Uri url, string date)
          {
          Dictionary StoredDates = new Dictionary();
          StoredDates = JsonConvert.DeserializeObject>(StoredDatesList);
          var contents = string.Empty;
          HttpClient client = new HttpClient();

                  if (StoredDates != null)
                      if (StoredDates.ContainsKey(date))
                      {
                          contents = StoredDates\[date\];
                      }
                      else
                      {
                          var current = Connectivity.NetworkAccess;
                          if (current != NetworkAccess.Internet)
                              return null;
                          client = new HttpClient();
                          contents = await client.GetStringAsync(url);
                          var res2 = JsonConvert.DeserializeObject(contents.ToString());
                          if (180 == res2.content.Count)
                          {
                              StoredDates.Add(date, contents);
                              StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented);
                          }
                      }
                  else
                  {
                      StoredDates = new Dictionary();
          
                      contents = await client.GetStringAsync(url);
                      var res2 = JsonConvert.DeserializeObject(contents.ToString());
                      if (180 == res2.content.Count)
                      {
                          StoredDates.Add(date, contents);
                          StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented);
                      }
                  }
                  return contents;
              }
          

          the if statement

          current != NetworkAccess.Internet)

          checks if internet is available, when internet is not available i return null and i check if the data is null and im displaying a message(error, internet is not available etc). I find this approach very bad and im trying to think how is the proper way to handle this. i cannot show a message t

          G Offline
          G Offline
          GuyThiebaut
          wrote on last edited by
          #4

          Slightly off topic perhaps, but you are repeating the same block of code twice and could refactor all of this into a single function and just call that function twice:

                      contents = await client.GetStringAsync(url);
                      var res2 = JsonConvert.DeserializeObject(contents.ToString());
                      if (180 == res2.content.Count)
                      {
                          StoredDates.Add(date, contents);
                          StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented);
                      }
          
          “That which can be asserted without evidence, can be dismissed without evidence.”

          ― Christopher Hitchens

          E 1 Reply Last reply
          0
          • G GuyThiebaut

            Slightly off topic perhaps, but you are repeating the same block of code twice and could refactor all of this into a single function and just call that function twice:

                        contents = await client.GetStringAsync(url);
                        var res2 = JsonConvert.DeserializeObject(contents.ToString());
                        if (180 == res2.content.Count)
                        {
                            StoredDates.Add(date, contents);
                            StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented);
                        }
            
            “That which can be asserted without evidence, can be dismissed without evidence.”

            ― Christopher Hitchens

            E Offline
            E Offline
            Exoskeletor
            wrote on last edited by
            #5

            Oh thank you. Any improvement is highly welcome, i have refactor it to:

            public static string StoredDatesList
            {
            get => Preferences.Get(nameof(StoredDatesList), string.Empty);
            set => Preferences.Set(nameof(StoredDatesList), value);
            }
            public static async Task GetDraws(Uri url, string date)
            {
            var StoredDates = JsonConvert.DeserializeObject>(StoredDatesList);
            var contents = string.Empty;
            var current = Connectivity.NetworkAccess;
            var client = new HttpClient();

                    if (StoredDates != null)
                        if (StoredDates.ContainsKey(date))
                        {
                            contents = StoredDates\[date\];
                        }
                        else
                        {
                            if (current != NetworkAccess.Internet)
                                return Helpers.Settings.Common\_Error\_NoInternetConnection;
            
                            contents = await DownloadResults(url, date, StoredDates, contents, client);
                        }
                    else
                    {
                        if (current != NetworkAccess.Internet)
                            return Helpers.Settings.Common\_Error\_NoInternetConnection;
            
                        StoredDates = new Dictionary();
                        contents = await DownloadResults(url, date, StoredDates, contents, client);
                    }
                    return contents;
                }
            
                private static async Task DownloadResults(Uri url, string date, Dictionary StoredDates, string contents, HttpClient client)
                {
                    contents = await client.GetStringAsync(url);
                    var res2 = JsonConvert.DeserializeObject(contents.ToString());
                    if (180 == res2.content.Count)
                    {
                        StoredDates.Add(date, contents);
                        StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented);
                    }
            
                    return contents;
                }
            

            ...
            public const string Common_Error_NoInternetConnection = "Error_NoInternetConnection";

            So i would check every time if the return sting is equal to Common_Error_NoInternetConnection, Does this sounds like a solid idea?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups