Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 31st, 2005, 12:57 PM   #1
Arla
Professional Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 301
Rep Power: 4 Arla is on a distinguished road
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...
Arla is online now   Reply With Quote
Old Mar 31st, 2005, 6:36 PM   #2
Arla
Professional Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 301
Rep Power: 4 Arla is on a distinguished road
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?
Arla is online now   Reply With Quote
Old Apr 1st, 2005, 11:28 AM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Quote:
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).
That's the idea... if it went through the try block without throwing an error, it would work correctly... otherwise, the programmer needs to know the who, what, when, where, and how of the error.


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."
Infinite Recursion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:57 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC