Best practice for Error Handling in ASP.Net and C#

R

RonL

What is the recommended best technique for handling errors/exceptions in
ASP.Net. I've read about the following techniques:

1. Try/Catch

2. Page_Error

3. Application_Error in the glabal.asax

4. Custom errors in Web.Config.


I'm probably going to go with #1 (Try/Catch) but was wondering about the
pros and cons of #2 and #3.

Thanks,
Ron
 
G

Guest

Use try/ catch /finally semantics anywhere in your code that an exception
could possibly occur.

Use a global Application_Error implementation to take care of unhandled
exceptions.

Write your code defensively so that you take every effort NOT to have an
exception.

Peter
 
R

RonL

Thanks for the response. Combining Try/Catch with Application_Error sounds
good. I'm working on an "in development" application inherited from an
acquired company. I have a question about Application_Error, does one have
access to the Response object so as to redirect to an error screen?

Thanks,
Ron
 
R

RonL

Thanks for the links. I noticed one of the links it talked about using
reflection to retrieve the data from Exception object. Have you seen any
examples on how to do that?

Thanks,
Ron
 
K

Kevin Spencer

Hi Ron,
Have you seen any examples on how to do that?

Here's a function I wrote for our applications, which I use in conjunction
with an error-handling utility. Remarks follow:

private static void GetExceptionDetails(Exception ex, ref StringBuilder sb)
{
int i;
sb.Append(sb.Length == 0 ? "" : nl);
Type t = ex.GetType();
Type[] types = new Type[] {
typeof(WebException),
typeof(SqlException),
typeof(SmtpException),
typeof(SmtpFailedRecipientException),
typeof(SmtpFailedRecipientsException),
typeof(ConfigurationException),
typeof(FileNotFoundException),
typeof(ProtocolViolationException)
};

string TypeName = t.ToString();

// Get type of Exception
for (i = 0; i < types.Length; i++)
{
if (t.Equals(types))
break;
}
sb.Append("Exception of Type \"" + TypeName + "\"" + nl);

// Handle with the appropriate handler
switch (i)
{
case 0: // WebException
GetWebException((WebException)ex, ref sb);
break;
case 1: // SqlException
GetSqlException((SqlException)ex, ref sb);
break;
case 2: // SmtpException
GetSmtpException((SmtpException)ex, ref sb);
break;
case 3: // SmtpFailedRecipientException
GetSmtpFailedRecipientException(
(SmtpFailedRecipientException)ex, ref sb);
break;
case 4: // SmtpFailedRecipientsException
GetSmtpFailedRecipientsException(
(SmtpFailedRecipientsException)ex, ref sb);
break;
case 5: // System.Configuration.ConfigurationException
GetConfigurationException(
(ConfigurationException)ex, ref sb);
break;
case 6:
GetFileNotFoundException(
(FileNotFoundException)ex, ref sb);
break;
case 7:
GetProtocolViolationException(
(ProtocolViolationException)ex, ref sb);
break;
default:
GetSystemException(ex, ref sb);
break;
}
if (ex.Data != null && ex.Data.Count > 0)
{
sb.Append(nl + "Data:");
foreach (DictionaryEntry d in ex.Data)
sb.Append(nl + "Key: " + d.Key.ToString() +
", Value: " + d.Value.ToString());
}

if (ex.InnerException != null)
{
sb.Append(nl + "InnerException: " + nl);
GetExceptionDetails(ex.InnerException, ref sb);
}
sb.Append(nl + "StackTrace: " + nl + ex.StackTrace);
if (ex.HelpLink != null && ex.HelpLink != "")
sb.Append(nl + "HelpLink: " + ex.HelpLink);
}

Basically, this method is passed a Stringbuilder by reference. It then looks
at the type of the Exception, and calls any of several overloads, depending
upon the Exception's type. It then looks at the InnerException, and if any
exists, it calls itself recursively for the InnerException. What it returns
is a Stringbuilder with a formatted string that can be logged in any way.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
R

RonL

Thanks for the example. I'll check it out.

Ron

Kevin Spencer said:
Hi Ron,
Have you seen any examples on how to do that?

Here's a function I wrote for our applications, which I use in conjunction
with an error-handling utility. Remarks follow:

private static void GetExceptionDetails(Exception ex, ref StringBuilder
sb)
{
int i;
sb.Append(sb.Length == 0 ? "" : nl);
Type t = ex.GetType();
Type[] types = new Type[] {
typeof(WebException),
typeof(SqlException),
typeof(SmtpException),
typeof(SmtpFailedRecipientException),
typeof(SmtpFailedRecipientsException),
typeof(ConfigurationException),
typeof(FileNotFoundException),
typeof(ProtocolViolationException)
};

string TypeName = t.ToString();

// Get type of Exception
for (i = 0; i < types.Length; i++)
{
if (t.Equals(types))
break;
}
sb.Append("Exception of Type \"" + TypeName + "\"" + nl);

// Handle with the appropriate handler
switch (i)
{
case 0: // WebException
GetWebException((WebException)ex, ref sb);
break;
case 1: // SqlException
GetSqlException((SqlException)ex, ref sb);
break;
case 2: // SmtpException
GetSmtpException((SmtpException)ex, ref sb);
break;
case 3: // SmtpFailedRecipientException
GetSmtpFailedRecipientException(
(SmtpFailedRecipientException)ex, ref sb);
break;
case 4: // SmtpFailedRecipientsException
GetSmtpFailedRecipientsException(
(SmtpFailedRecipientsException)ex, ref sb);
break;
case 5: // System.Configuration.ConfigurationException
GetConfigurationException(
(ConfigurationException)ex, ref sb);
break;
case 6:
GetFileNotFoundException(
(FileNotFoundException)ex, ref sb);
break;
case 7:
GetProtocolViolationException(
(ProtocolViolationException)ex, ref sb);
break;
default:
GetSystemException(ex, ref sb);
break;
}
if (ex.Data != null && ex.Data.Count > 0)
{
sb.Append(nl + "Data:");
foreach (DictionaryEntry d in ex.Data)
sb.Append(nl + "Key: " + d.Key.ToString() +
", Value: " + d.Value.ToString());
}

if (ex.InnerException != null)
{
sb.Append(nl + "InnerException: " + nl);
GetExceptionDetails(ex.InnerException, ref sb);
}
sb.Append(nl + "StackTrace: " + nl + ex.StackTrace);
if (ex.HelpLink != null && ex.HelpLink != "")
sb.Append(nl + "HelpLink: " + ex.HelpLink);
}

Basically, this method is passed a Stringbuilder by reference. It then
looks at the type of the Exception, and calls any of several overloads,
depending upon the Exception's type. It then looks at the InnerException,
and if any exists, it calls itself recursively for the InnerException.
What it returns is a Stringbuilder with a formatted string that can be
logged in any way.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

RonL said:
Thanks for the links. I noticed one of the links it talked about using
reflection to retrieve the data from Exception object. Have you seen any
examples on how to do that?

Thanks,
Ron
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top