Real example of exception handling

J

Justin Dutoit

Hey folks. After downloading and reading a lot of code, I'm still yet to
find a complete, real-world example of try... catch blocks in the data layer
of an application (handling a few types of exceptions). Does anyone know a
website with some code, which has thorough best practice exception handling?
Or perhaps someone could reply with some example code. I appreciate your
help very much,

Cheers,
Justin Dutoit
 
M

Morten Wennevik [C# MVP]

Justin Dutoit said:
Hey folks. After downloading and reading a lot of code, I'm still yet to
find a complete, real-world example of try... catch blocks in the data layer
of an application (handling a few types of exceptions). Does anyone know a
website with some code, which has thorough best practice exception handling?
Or perhaps someone could reply with some example code. I appreciate your
help very much,

Cheers,
Justin Dutoit

Hi Justin,

I'm not sure what you are looking for. Generally you don't catch exceptions
in the data layer unless you need to wrap them in a custom exception type,
log the errors before passing them on, or similar. Exceptions should bubble
up to the layers above. There is nothing mysterious about catching
exceptions in the data layer as opposed to other layers, though the reason
why there are so few examples may be because if something goes wrong here,
this isn't the place to handle the errors.

If you want to read about how to do exception handling, you may be better
off reading about how not to do it. Anyhow, not particularly real world:

[Exceptions and Exception Handling (C# Programming Guide)]
http://msdn.microsoft.com/en-us/library/ms173160.aspx

[Exception Handling Best Practices in .NET]
http://www.codeproject.com/KB/architecture/exceptionbestpractices.aspx
 
J

Justin Dutoit

Hi. This is what I'm doing in the data layer at the moment:

try
{
myConnection.Open();
myDataAdapter.Fill(myDataSet);
}
catch (SqlException exc)
{
ExceptionLog.Add(exc);
throw;
}
catch (Exception exc)
{
ExceptionLog.Add(exc);
throw;
}
finally
{
myConnection.Close();
}

What code should be in the layers above to handle any error? Also pl advise
if the above code needs anything...

Tks
Justin




Morten Wennevik said:
Justin Dutoit said:
Hey folks. After downloading and reading a lot of code, I'm still yet to
find a complete, real-world example of try... catch blocks in the data
layer
of an application (handling a few types of exceptions). Does anyone know
a
website with some code, which has thorough best practice exception
handling?
Or perhaps someone could reply with some example code. I appreciate your
help very much,

Cheers,
Justin Dutoit

Hi Justin,

I'm not sure what you are looking for. Generally you don't catch
exceptions
in the data layer unless you need to wrap them in a custom exception type,
log the errors before passing them on, or similar. Exceptions should
bubble
up to the layers above. There is nothing mysterious about catching
exceptions in the data layer as opposed to other layers, though the reason
why there are so few examples may be because if something goes wrong here,
this isn't the place to handle the errors.

If you want to read about how to do exception handling, you may be better
off reading about how not to do it. Anyhow, not particularly real world:

[Exceptions and Exception Handling (C# Programming Guide)]
http://msdn.microsoft.com/en-us/library/ms173160.aspx

[Exception Handling Best Practices in .NET]
http://www.codeproject.com/KB/architecture/exceptionbestpractices.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top