By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,364 Members | 1,721 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,364 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Apr 1 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
See :

http://msdn.microsoft.com/library/de...Exceptions.asp

http://msdn.microsoft.com/library/de...ceptdotnet.asp

http://www.c-sharpcorner.com/Code/20...Management.asp


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"RonL" <ro***@hotmail.com> wrote in message news:NV*******************@newssvr29.news.prodigy. net...
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 global.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


Apr 1 '06 #2

P: n/a
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
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"RonL" wrote:
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

Apr 1 '06 #3

P: n/a
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

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:A7**********************************@microsof t.com...
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
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"RonL" wrote:
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

Apr 1 '06 #4

P: n/a
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

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:OB**************@TK2MSFTNGP14.phx.gbl...
See :

http://msdn.microsoft.com/library/de...Exceptions.asp

http://msdn.microsoft.com/library/de...ceptdotnet.asp

http://www.c-sharpcorner.com/Code/20...Management.asp


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"RonL" <ro***@hotmail.com> wrote in message
news:NV*******************@newssvr29.news.prodigy. net...
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 global.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


Apr 1 '06 #5

P: n/a
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[i]))
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" <ro***@hotmail.com> wrote in message
news:av*******************@newssvr12.news.prodigy. com... 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

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:OB**************@TK2MSFTNGP14.phx.gbl...
See :

http://msdn.microsoft.com/library/de...Exceptions.asp

http://msdn.microsoft.com/library/de...ceptdotnet.asp

http://www.c-sharpcorner.com/Code/20...Management.asp


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"RonL" <ro***@hotmail.com> wrote in message
news:NV*******************@newssvr29.news.prodigy. net...
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 global.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



Apr 1 '06 #6

P: n/a
Thanks for the example. I'll check it out.

Ron

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:Ow****************@TK2MSFTNGP09.phx.gbl...
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[i]))
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" <ro***@hotmail.com> wrote in message
news:av*******************@newssvr12.news.prodigy. com...
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

"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:OB**************@TK2MSFTNGP14.phx.gbl...
See :

http://msdn.microsoft.com/library/de...Exceptions.asp

http://msdn.microsoft.com/library/de...ceptdotnet.asp

http://www.c-sharpcorner.com/Code/20...Management.asp


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"RonL" <ro***@hotmail.com> wrote in message
news:NV*******************@newssvr29.news.prodigy. net...
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 global.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



Apr 3 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.