Reply
Dealing with exceptions
Old 02-19-2007, 03:18 PM Dealing with exceptions
Learning Newbie's Avatar
Moderator

Posts: 5,199
Name: John Alexander
void Page_Load(object sender, EventArgs e) {
try {
// Do something ...
} catch(Exception ex) {
string msg = "<h3>" + ex.Message + "</h3>";
msg += ex.StackTrace.Replace(" at ", "<br>");

if(ex.InnerException != null) {
msg += "<br><br><b>Caused By:</b><br><br><h3>" + ex.InnerExeption.Message + "</h3>";
msg += ex.InnerException.StackTrace.Replace(" at ", "<br>");
}
// Now log msg to the database, and it will be rendered properly for an administrator who can view an online website health report

}
}
Learning Newbie is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 02-19-2007, 03:24 PM Re: Dealing with exceptions
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,945
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Sorry dude...this looks helpful but someone might get confused by this, so forgive my anal nature:
Code:
msg += "<br><br><b>Caused By:</b><br><br><h3>" + ex.InnerException.Message + "</h3>";
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 02-20-2007, 03:14 PM Re: Dealing with exceptions
Learning Newbie's Avatar
Moderator

Posts: 5,199
Name: John Alexander
Good catch, thanks! I knew I should have pulled up the IDE instead of just typing that in here.

Anybody else can use the code Adam and I pieced together in your systems. Users hate errors, and expect you to fix them in a hurry. Under the defaults if you're running ASP.NET it will only show what caused the error if you're using the server to surf to that page and get an error - from a different machine you get a page that says "Oops!" But the cause of the error is available in these variables, except they use line breaks, which get ignored when you render them with html. So we simply force a <br> where you would expect a line break in the report, to make it easier to read.

For extra credit, you could set this to email you whenever an error happens, with the details of what caused it. Then you might be able to fix some of them before your users even complain.
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Dealing with exceptions
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.12659 seconds with 12 queries