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. Error handling - which is preferrable?

Error handling - which is preferrable?

Scheduled Pinned Locked Moved C#
htmlcomsecurityhelptutorial
4 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.
  • J Offline
    J Offline
    Judah Gabriel Himango
    wrote on last edited by
    #1

    I'm calling a method that can throw one of several exceptions:

    byte[] ReadFile(string filePath)
    {
    return File.ReadAllBytes(filePath);
    }

    When calling this method, do you guys usually catch every possible exception, or just cover it with a single catch block? For example, do you do this:

    string errorMessage = null;
    try
    {
    return File.ReadAllBytes(filePath);
    }
    catch (ArgumentException argumentError)
    {
    errorMessage = argumentError.Message;
    }
    catch (ArgumentNullException nullArgumentError)
    {
    errorMessage = nullArgumentError.Message;
    }
    catch (PathTooLongException pathError)
    {
    errorMessage = pathError.Message;
    }
    catch (DirectoryNotFoundException directoryNotFoundError)
    {
    errorMessage = directoryNotFoundError.Message;
    }
    catch (IOException ioError)
    {
    errorMessage = ioError.Message;
    }
    catch (UnauthorizedAccessException unauthorizedError)
    {
    errorMessage = unauthorizedError.Message;
    }
    catch (FileNotFoundException fileNotFoundError)
    {
    errorMessage = fileNotFoundError.Message;
    }
    catch (NotSupportedException notSupportedError)
    {
    errorMessage = notSupportedError;
    }
    catch (System.Security.SecurityException securityError)
    {
    errorMessage = securityError;
    }

            if(errorMessage != null)
            {
                MessageBox.Show(errorMessage, ...);
            }
    

    Or do you catch it all in a single System.Exception catch block?

    try
    {
    File.ReadAllBytes(filePath);
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message, ...);
    }

    Obviously the latter is easier to read and write, but the former is more precise and won't eat up things like system errors (OutOfMemoryException, OverflowException, etc.). Which do you guys suggest?

    Tech, life, family, faith: Give me a visit. I'm currently blogging about: Little House on the Flickr Judah Himango<

    C 1 Reply Last reply
    0
    • J Judah Gabriel Himango

      I'm calling a method that can throw one of several exceptions:

      byte[] ReadFile(string filePath)
      {
      return File.ReadAllBytes(filePath);
      }

      When calling this method, do you guys usually catch every possible exception, or just cover it with a single catch block? For example, do you do this:

      string errorMessage = null;
      try
      {
      return File.ReadAllBytes(filePath);
      }
      catch (ArgumentException argumentError)
      {
      errorMessage = argumentError.Message;
      }
      catch (ArgumentNullException nullArgumentError)
      {
      errorMessage = nullArgumentError.Message;
      }
      catch (PathTooLongException pathError)
      {
      errorMessage = pathError.Message;
      }
      catch (DirectoryNotFoundException directoryNotFoundError)
      {
      errorMessage = directoryNotFoundError.Message;
      }
      catch (IOException ioError)
      {
      errorMessage = ioError.Message;
      }
      catch (UnauthorizedAccessException unauthorizedError)
      {
      errorMessage = unauthorizedError.Message;
      }
      catch (FileNotFoundException fileNotFoundError)
      {
      errorMessage = fileNotFoundError.Message;
      }
      catch (NotSupportedException notSupportedError)
      {
      errorMessage = notSupportedError;
      }
      catch (System.Security.SecurityException securityError)
      {
      errorMessage = securityError;
      }

              if(errorMessage != null)
              {
                  MessageBox.Show(errorMessage, ...);
              }
      

      Or do you catch it all in a single System.Exception catch block?

      try
      {
      File.ReadAllBytes(filePath);
      }
      catch(Exception ex)
      {
      MessageBox.Show(ex.Message, ...);
      }

      Obviously the latter is easier to read and write, but the former is more precise and won't eat up things like system errors (OutOfMemoryException, OverflowException, etc.). Which do you guys suggest?

      Tech, life, family, faith: Give me a visit. I'm currently blogging about: Little House on the Flickr Judah Himango<

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      It depends. For example, when reading a file I'll normally check it exists first, so I a PathTooLongException, DirectoryNotFoundException and FileNotFound are really things I should not be getting, so they'd be covered in a catch all (if at all at that point in the code). Specific things that I can't check for in advance would be in specific blocks. On the whole I find that exceptions propogate up a couple of method calls in the stack before I actually handle them because often at the point the exception occurs I am not able to handle it so I allow it to propogate up until I can do something about it. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

      W J 2 Replies Last reply
      0
      • C Colin Angus Mackay

        It depends. For example, when reading a file I'll normally check it exists first, so I a PathTooLongException, DirectoryNotFoundException and FileNotFound are really things I should not be getting, so they'd be covered in a catch all (if at all at that point in the code). Specific things that I can't check for in advance would be in specific blocks. On the whole I find that exceptions propogate up a couple of method calls in the stack before I actually handle them because often at the point the exception occurs I am not able to handle it so I allow it to propogate up until I can do something about it. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

        W Offline
        W Offline
        Werdna
        wrote on last edited by
        #3

        Or in case of IO operation it would be better to do: catch (IOException ex) { .. io error occured. PathTooLongException and all other IO related inherit from IOException } catch (Exception ex) { .. all other unexpected errors }

        1 Reply Last reply
        0
        • C Colin Angus Mackay

          It depends. For example, when reading a file I'll normally check it exists first, so I a PathTooLongException, DirectoryNotFoundException and FileNotFound are really things I should not be getting, so they'd be covered in a catch all (if at all at that point in the code). Specific things that I can't check for in advance would be in specific blocks. On the whole I find that exceptions propogate up a couple of method calls in the stack before I actually handle them because often at the point the exception occurs I am not able to handle it so I allow it to propogate up until I can do something about it. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          My example was simplified; my real code has the error propagating up a ways until it gets to the UI as well.

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: Little House on the Flickr Judah Himango

          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