-
Apr 23rd, 2014, 08:35 AM
#1
Thread Starter
Frenzied Member
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?
-
Apr 23rd, 2014, 09:51 AM
#2
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
-
Apr 23rd, 2014, 10:36 AM
#3
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
-
Apr 23rd, 2014, 10:59 AM
#4
Frenzied Member
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.
-
Apr 23rd, 2014, 03:43 PM
#5
Re: How to know which Exceptions to handle
Originally Posted by dolot
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.
-
Apr 23rd, 2014, 04:16 PM
#6
Re: How to know which Exceptions to handle
Originally Posted by ident
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
-
Apr 23rd, 2014, 05:09 PM
#7
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
-
Apr 23rd, 2014, 05:18 PM
#8
Re: How to know which Exceptions to handle
Originally Posted by dolot
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.
-
Apr 23rd, 2014, 05:26 PM
#9
Re: How to know which Exceptions to handle
That's an excellent link. This time, I stored the link.
My usual boring signature: Nothing
-
Apr 24th, 2014, 08:13 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|