![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Mar 2005
Posts: 301
Rep Power: 4
![]() |
Error Handling in C#
So,
This is a general discussion rather than anything specific, I'm trying to setup some C# programs, but I'm trying to generalize my error handling. My two basic issues right now are trying to work out when to use the try, catch and finally blocks of code, do you use it for everything, or only those lines you "expect" an error to occur. Secondly, if you use try, catch and finally blocks, presumably as soon as an error is encountered in the try block, you skip straight to the catch block (not the best in the world, as it makes code very sensative to try statements). Also I read that exceptions are fairly bad for a lot of things, since they are relatively expensive operations, so I'm trying to come up with more generic standards to use for "errors" that aren't exceptions (for example, my program currently reads from a database, things like database not found would be an exception, things like row not found (for a read action) would just be an error, since I don't want to create an exception every time something isn't found, especially since quite often I don't expect data (do a read to check that you aren't creating a duplicate). So... any thoughts? Not sure if this isn't too general a question, I'm interested in what others have done, do you have try/catch/finally blocks sprinkled throughout your code, or do you try to block stuff off or... |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Mar 2005
Posts: 301
Rep Power: 4
![]() |
To followon, for example
I have a public variable called Description, the code for it goes something like this public string Description
{
get {return pDescription;}
set {
bool flag = false;
char [] a_character = value.ToCharArray();
//Check that every character isn't a space
for (int i=0;i<value.Length;i++)
{
if (!a_character[i].Equals(' '))
{
flag = true;
break;
}
}
//Check that the description contains some characters
if (value.Length == 0 | value==null)
{
throw new System.ArgumentNullException("Description","Description cannot be null");
}
else if (value.Length > 255)
{
throw new System.ArgumentOutOfRangeException("Description","Description must be less than 256 characters");
}
else if (flag == false)
{
throw new System.ArgumentOutOfRangeException("Description","Description must contain something other than spaces");
}
else
{
pDescription = value;
}
}
}This works, but to me looks ugly, and seems bad, I'd prefer not to have throw statements scattered throughout my code, but I'm not entirely sure what alternatives I have, anyone see other things or have better ideas? |
|
|
|
|
|
#3 | |
|
Programming Guru
![]() ![]() ![]() |
Quote:
only use error handlers when you are not certain if the code will execute as expected... you could also have a debug mode where extensive error handling could be toggled.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|