Results 1 to 10 of 10

Thread: How to know which Exceptions to handle

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    How to know which Exceptions to handle

    I am designing a multi-user application and I am trying to think of scenarios where exceptions can occur. One example of an exception that can occur is a concurrency violation. I know this can happen because of various types of tests I have run in multi-user environment as well as reading up on ADO.NET. Another example of an exception that can occur, is file selection using Open File Dialog, where you have to make sure that user did not select cancel.

    Without this previous knowledge of scenarios however, I would not know off the top of my head that this exception can occur. Is there a standard way, or for lack of a better word, a 'checklist' that you run through while creating error handling? Or is it up to the developer to in essence understand what should happen in the logic vs what could happen? How do you go about knowing the different types of exceptions from any given scenario?

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: How to know which Exceptions to handle

    Database access, file access, conversions.... those are the top 3 I usually deal with ... after that, it's anything that crops up during development or testing. More often than not though, I try to prevent the exceptions fronm happening in the first place when I can help it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,253

    Re: How to know which Exceptions to handle

    The one that often nails me is 'NullReferenceException' - an object being nothing when I expect it to be something. As TG said, that often appears in testing, but I'm learning to head it off in advance.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  4. #4
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,373

    Re: How to know which Exceptions to handle

    Not receiving information that you expect to receive such as a return file path from an Open File Dialogue or a null value returned from a database are things that ought to be captured and handled in your programme logic rather than being dealt with via exception handling. Try and anticipate as many of these things as possible in your code and capture any unforeseen problems within your Try block. I always display error numbers as well as messages so that if a user reports an error then I have all the information that I need to fix the error or handle it better with my programme logic.

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: How to know which Exceptions to handle

    Quote Originally Posted by dolot View Post
    The one that often nails me is 'NullReferenceException' - an object being nothing when I expect it to be something. As TG said, that often appears in testing, but I'm learning to head it off in advance.
    NullReference should be tested for, not left to a catch block.

  6. #6
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,253

    Re: How to know which Exceptions to handle

    Quote Originally Posted by ident View Post
    NullReference should be tested for, not left to a catch block.
    That's what I do - I don't use a try-catch block for it. I guess I should have been more clear on that. In fact, just about anything that can generate an error can be checked for in advance without using a try-catch block - if you know what you're looking for. And as tg said, a lot of that stuff manifests itself in testing.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: How to know which Exceptions to handle

    Null references are probably the single most common exception I encounter during testing, but I never try to handle them, which is probably what dolot meant, as well. Every one is a bug, and usually a boring one, but they are easy to fix.
    My usual boring signature: Nothing

  8. #8
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: How to know which Exceptions to handle

    Quote Originally Posted by dolot View Post
    In fact, just about anything that can generate an error can be checked for in advance without using a try-catch block - if you know what you're looking for.
    Not so. I've posted this link before: http://blogs.msdn.com/b/ericlippert/...edirected=true

    Only those in the "boneheaded" bucket can be tested for in advance. Note that FileNotFoundException is not a boneheaded exception, even though you can check for the file's existence before trying to open it.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: How to know which Exceptions to handle

    That's an excellent link. This time, I stored the link.
    My usual boring signature: Nothing

  10. #10
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,253

    Re: How to know which Exceptions to handle

    Yes, the file access issue is a good one for a try-catch. While it's highly unlikely that a file would get deleted in the few microseconds that exist between checking for a file's existence and trying to open it, there are other file access issues that can come into play, and checking for all of them in advance gets messy. It that case it's cleaner to just try to open the file and then catch any exceptions and then handle them appropriately.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width