Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 30th, 2013, 01:03 PM
Friend of Wrox
Points: 595, Level: 8
Points: 595, Level: 8 Points: 595, Level: 8 Points: 595, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2011
Posts: 126
Thanks: 39
Thanked 2 Times in 2 Posts
Default Exception Handling Tip - Ch. 18, p.695

Hi Imar,

I'm reviewing the tips at the end of Ch. 18 and am trying to get a better understanding of the last tip,

Quote:
"Be as explicit as possible with the Exception types you catch in Try/Catch blocks. Try to avoid catching generic Exception types and set up multiple, explicit Catch blocks for each specific type you anticipate."
Using the exercise on "Handling Exceptions" and the ContactForm.ascx (p. 663) as an example, how would you know what specific exceptions to catch for sending emails and how would you change this example?

Thanks,
Tulsi
Reply With Quote
  #2 (permalink)  
Old May 30th, 2013, 02:25 PM
Imar's Avatar
Wrox Author
Points: 72,019, Level: 100
Points: 72,019, Level: 100 Points: 72,019, Level: 100 Points: 72,019, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,075
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Tulsi,

Take a look at page 660 for a concrete example. That code catches an SmtpException as well as a generic Exception. You could further divide this by catching SmtpFailedRecipientException and SmtpFailedRecipientsException:

http://msdn.microsoft.com/en-us/libr...exception.aspx

To find out the Exceptions that can be thrown, consult the MSDN documentation. Here's an example of the Send method of the SmtpClient class: http://msdn.microsoft.com/en-us/library/swas0fwc.aspx

In this case, I would probably just handle SmtpException as all you want to do is tell the user something went wrong while sending the message. The first three exceptions can typically be avoided by constructing a valid message object.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #3 (permalink)  
Old June 5th, 2013, 01:17 PM
Friend of Wrox
Points: 595, Level: 8
Points: 595, Level: 8 Points: 595, Level: 8 Points: 595, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2011
Posts: 126
Thanks: 39
Thanked 2 Times in 2 Posts
Default

Hi Imar,

Thank you for directing me to this link!

Until now, I've always been confused as to how one would know what exceptions to look for. This link surely clarifies things.

After reviewing ContactForm.acx again, I noticed that there are more exceptions that may be generated in the following code in SendButton_Click() when trying to read the ContactForm.txt file:

Code:
if (string.IsNullOrEmpty(mailBody))
            {
                string fileName = Server.MapPath("~/App_Data/ContactForm.txt");
                mailBody = File.ReadAllText(fileName);
                Cache.Insert("ContactFormMailBody", mailBody, new System.Web.Caching.CacheDependency(fileName));                
            }
Should this code be wrapped in it's own try/catch block separate from the try/catch block for sending the actual email? What is the correct way to handle this type of situation?

Tulsi
Reply With Quote
  #4 (permalink)  
Old June 6th, 2013, 04:11 AM
Imar's Avatar
Wrox Author
Points: 72,019, Level: 100
Points: 72,019, Level: 100 Points: 72,019, Level: 100 Points: 72,019, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,075
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Tulsi,

It depends ;-)

In my opinion, the fact that the mail body file can't be found or read is a software or configuration error that won't go away automatically. This means there's no point in showing the user a message like "Ooops, something went wrong; please try again:.

Instead, you want to be notified of this error using the global error handler in Global.asax (or better yet, with ELMAH). The easiest thing to do is not handle this exception in the contact control, but rely on custom errors. This way, the user gets the generic Error 500 page and the error message is logged.

In general, you should catch exceptions that you know how to handle, and let the others bubble up as unhandled exceptions and log them at the global level.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Tulsi (June 6th, 2013)
  #5 (permalink)  
Old June 6th, 2013, 09:33 AM
Friend of Wrox
Points: 595, Level: 8
Points: 595, Level: 8 Points: 595, Level: 8 Points: 595, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2011
Posts: 126
Thanks: 39
Thanked 2 Times in 2 Posts
Default

This was very helpful:) Thank you so much for the detailed and quick replies.

This has made the learning process so much easier and exciting!

I'm continuing to the chapter on "Deploying your Web Site" - Very excited!!

Tulsi
Reply With Quote
  #6 (permalink)  
Old June 6th, 2013, 10:34 AM
Imar's Avatar
Wrox Author
Points: 72,019, Level: 100
Points: 72,019, Level: 100 Points: 72,019, Level: 100 Points: 72,019, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,075
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

>> I'm continuing to the chapter on "Deploying your Web Site" - Very excited!!

Excellent. Almost done. Congratulations.... ;-)

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
ch 18 - Try It out: Handling Exceptions Site Wide Tulsi BOOK: Beginning ASP.NET 4 : in C# and VB 10 September 12th, 2013 01:30 PM
Exception Handling Fed BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 4 February 16th, 2010 05:49 PM
Exception handling codehelp C# 2008 aka C# 3.0 4 August 27th, 2009 07:46 AM
Exception Handling NewTitle2007 BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 6 August 8th, 2007 04:03 AM
Exception handling aldwinenriquez General .NET 0 August 17th, 2005 06:13 PM



All times are GMT -4. The time now is 02:02 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.