Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Exception Handling Question - Again
Message
From
23/10/2009 00:22:49
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01430826
Message ID:
01430874
Views:
39
>Coming from you that makes me feel much better! Thanks!

You're welcome! There are other options as well, obviously.

I probably should have mentioned this in my first reply, but the main downside to this solution is that all your calls to these methods need to remember to check the oException property ... so all your developers need to be aware of this or it could cause problems.

Our Framework initially worked this way (in fact, I've frequently posted some code for a DataAccess base class that does this, or something similar), but as we hired more and more developers, we wanted something a little more fool-proof; so we ended up refactoring to utilize anonymous delegates. I don't have any sample of that methodology thrown together at this time, but perhaps I should do that some day.

~~Bonnie


>
>
>
>>There are probably many different "right ways" ... your solution is perfectly ok.
>>
>>~~Bonnie
>>
>>
>>
>>
>>>I few weeks ago I asked a question about using an event to handle exceptions. Here's a follow up.
>>>
>>>Assume I have this method in my Data Access class:
>>>
>>>
>>>public int ExecuteNonQuery(string sCommand, ArrayList colParams, CommandType Type)
>>>{
>>>    int iRetVal = 0;
>>>
>>>    SqlConnection oConn = _GetConnection(true);
>>>
>>>    if (oException == null)
>>>    {
>>>        SqlCommand oCommand = new SqlCommand();
>>>        oCommand.CommandText = sCommand;
>>>        oCommand.Connection = oConn;
>>>
>>>        if (colParams != null)
>>>        {
>>>            SqlParameter[] aParams = new SqlParameter[colParams.Count];
>>>            colParams.CopyTo(aParams);
>>>
>>>            oCommand.Parameters.Clear();
>>>            oCommand.Parameters.AddRange(aParams);
>>>        }
>>>
>>>        oCommand.CommandType = Type;
>>>
>>>        try
>>>        {
>>>            iRetVal = oCommand.ExecuteNonQuery();
>>>        }
>>>        catch (SqlException e)
>>>        {
>>>            _oException = e;
>>>        }
>>>        finally
>>>        {
>>>            if (oConn.State == ConnectionState.Open)
>>>            {
>>>                oConn.Close();
>>>            }
>>>        }
>>>
>>>        if (_oException == null)
>>>        {
>>>            oCommand.Parameters.Clear();
>>>        }
>>>
>>>    }
>>>
>>>    return iRetVal;
>>>}
>>>
>>>
>>>You can see that this class uses a try/catch blocks to trap exceptions. If an exception is caught it is stored to the oException property. Then in the class that uses this I can check to see of the oException property is no longer null.
>>>
>>>What's the right way to do this?
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform