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_Err or 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 6 2088
Use try/ catch /finally semantics anywhere in your code that an exception
could possibly occur.
Use a global Application_Err or 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_Err or 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
Thanks for the response. Combining Try/Catch with Application_Err or sounds
good. I'm working on an "in development" application inherited from an
acquired company. I have a question about Application_Err or, does one have
access to the Response object so as to redirect to an error screen?
Thanks,
Ron
"Peter Bromberg [C# MVP]" <pb*******@yaho o.nospammin.com > wrote in message
news:A7******** *************** ***********@mic rosoft.com... Use try/ catch /finally semantics anywhere in your code that an exception could possibly occur.
Use a global Application_Err or 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_Err or 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
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 GetExceptionDet ails(Exception ex, ref StringBuilder sb)
{
int i;
sb.Append(sb.Le ngth == 0 ? "" : nl);
Type t = ex.GetType();
Type[] types = new Type[] {
typeof(WebExcep tion),
typeof(SqlExcep tion),
typeof(SmtpExce ption),
typeof(SmtpFail edRecipientExce ption),
typeof(SmtpFail edRecipientsExc eption),
typeof(Configur ationException) ,
typeof(FileNotF oundException),
typeof(Protocol ViolationExcept ion)
};
string TypeName = t.ToString();
// Get type of Exception
for (i = 0; i < types.Length; i++)
{
if (t.Equals(types[i]))
break;
}
sb.Append("Exce ption 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
GetSmtpExceptio n((SmtpExceptio n)ex, ref sb);
break;
case 3: // SmtpFailedRecip ientException
GetSmtpFailedRe cipientExceptio n(
(SmtpFailedReci pientException) ex, ref sb);
break;
case 4: // SmtpFailedRecip ientsException
GetSmtpFailedRe cipientsExcepti on(
(SmtpFailedReci pientsException )ex, ref sb);
break;
case 5: // System.Configur ation.Configura tionException
GetConfiguratio nException(
(ConfigurationE xception)ex, ref sb);
break;
case 6:
GetFileNotFound Exception(
(FileNotFoundEx ception)ex, ref sb);
break;
case 7:
GetProtocolViol ationException(
(ProtocolViolat ionException)ex , ref sb);
break;
default:
GetSystemExcept ion(ex, ref sb);
break;
}
if (ex.Data != null && ex.Data.Count > 0)
{
sb.Append(nl + "Data:");
foreach (DictionaryEntr y d in ex.Data)
sb.Append(nl + "Key: " + d.Key.ToString( ) +
", Value: " + d.Value.ToStrin g());
}
if (ex.InnerExcept ion != null)
{
sb.Append(nl + "InnerException : " + nl);
GetExceptionDet ails(ex.InnerEx ception, 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******** ***********@new ssvr12.news.pro digy.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******** ******@TK2MSFTN GP14.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******** ***********@new ssvr29.news.pro digy.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_Err or 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
Thanks for the example. I'll check it out.
Ron
"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:Ow******** ********@TK2MSF TNGP09.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 GetExceptionDet ails(Exception ex, ref StringBuilder sb) { int i; sb.Append(sb.Le ngth == 0 ? "" : nl); Type t = ex.GetType(); Type[] types = new Type[] { typeof(WebExcep tion), typeof(SqlExcep tion), typeof(SmtpExce ption), typeof(SmtpFail edRecipientExce ption), typeof(SmtpFail edRecipientsExc eption), typeof(Configur ationException) , typeof(FileNotF oundException), typeof(Protocol ViolationExcept ion) };
string TypeName = t.ToString();
// Get type of Exception for (i = 0; i < types.Length; i++) { if (t.Equals(types[i])) break; } sb.Append("Exce ption 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 GetSmtpExceptio n((SmtpExceptio n)ex, ref sb); break; case 3: // SmtpFailedRecip ientException GetSmtpFailedRe cipientExceptio n( (SmtpFailedReci pientException) ex, ref sb); break; case 4: // SmtpFailedRecip ientsException GetSmtpFailedRe cipientsExcepti on( (SmtpFailedReci pientsException )ex, ref sb); break; case 5: // System.Configur ation.Configura tionException GetConfiguratio nException( (ConfigurationE xception)ex, ref sb); break; case 6: GetFileNotFound Exception( (FileNotFoundEx ception)ex, ref sb); break; case 7: GetProtocolViol ationException( (ProtocolViolat ionException)ex , ref sb); break; default: GetSystemExcept ion(ex, ref sb); break; } if (ex.Data != null && ex.Data.Count > 0) { sb.Append(nl + "Data:"); foreach (DictionaryEntr y d in ex.Data) sb.Append(nl + "Key: " + d.Key.ToString( ) + ", Value: " + d.Value.ToStrin g()); }
if (ex.InnerExcept ion != null) { sb.Append(nl + "InnerException : " + nl); GetExceptionDet ails(ex.InnerEx ception, 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******** ***********@new ssvr12.news.pro digy.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******** ******@TK2MSFTN GP14.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******** ***********@new ssvr29.news.pro digy.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_Err or 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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kamilche |
last post by:
I'm totally new to Python error handling. I see there are many, many
ways to handle errors in Python. What is the standard accepted 'best
practice'?
I will be writing unit tests for every module, and translating into
different languages. If I call everything an 'Exception', I can easily
load the messages from a text file... if I finely...
|
by: Peter Foti |
last post by:
Simple question... which is better to use for defining font sizes and why?
px and em seem to be the leading candidates. I know what the general answer
is going to be, but I'm hoping to ultimately get some good real world
examples.
Fire away! :)
Regards,
Peter Foti
|
by: Mark Twombley |
last post by:
Hi, I'm just getting back into C++ and had a question about the best
practice for assigning error numbers.
I have been working in VB for sometime now and there you would start
assigning error number at vbObjectError + count.
Is there a similar practice in C++ or is it just coder preference.
Thanks
--
Mark Twombley
|
by: Matt Kruse |
last post by:
http://www.JavascriptToolbox.com/bestpractices/
I started writing this up as a guide for some people who were looking for
general tips on how to do things the 'right way' with Javascript. Their code
was littered with document.all and eval, for example, and I wanted to create
a practical list of best practices that they could easily put to...
|
by: Sandy |
last post by:
Hello -
I read an interesting article on the web wherein the author states he
doesn't handle too many errors at page level, but handles them at the
application level. He further goes on to show how error logging can be
incorporated using a Sql Server log with an additional text log in case Sql
Server was down.
My inclination after...
| |
by: James Radke |
last post by:
Hello,
I am looking for guidance on best practices to incorporate effective and complete error handling in an application written in VB.NET. If I have the following function in a class module (note that this class module represents the business layer of code NOT the gui layer):
Public Function Test(ByVal Parm1 As Integer, ByVal Parm2 As...
|
by: csgraham74 |
last post by:
Hi guys,
Basically i have been developing in dotnet for a couple of years but
ive had a few issues in regards to error handling.
For example - I have a class that i call passing in a stored procedure
and connection string as a path. My method returns a dataset. In my SP
i have an output parameter which tells me whether the SP select is...
|
by: =?Utf-8?B?QmlsbHk=?= |
last post by:
asp.net 2.0 vs2005
What is best practice for exception handling on a website in vs2005?
I was going to catch errors in application on_error event, log them to the
event log, then send users to an error page. But looking at the event log,
asp.net 2.0 logs those errors anyways, with more detail than i cen get from
the exception object. So is...
|
by: =?GB2312?B?17/HvyBaaHVvLCBRaWFuZw==?= |
last post by:
Hi,
I would like to have someone comments on what's the best practice
defining error codes in C.
Here's what I think:
solution A:
using enum
pros: type safe. better for debug (some debugger will show the name
not only the value)
cons: enum can not be forward declared which makes all error codes
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |