 |
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

May 30th, 2013, 01:03 PM
|
|
Friend of Wrox
|
|
Join Date: Mar 2011
Posts: 126
Thanks: 39
Thanked 2 Times in 2 Posts
|
|
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
|
|

May 30th, 2013, 02:25 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

June 5th, 2013, 01:17 PM
|
|
Friend of Wrox
|
|
Join Date: Mar 2011
Posts: 126
Thanks: 39
Thanked 2 Times in 2 Posts
|
|
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
|
|

June 6th, 2013, 04:11 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

June 6th, 2013, 09:33 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2011
Posts: 126
Thanks: 39
Thanked 2 Times in 2 Posts
|
|
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
|
|

June 6th, 2013, 10:34 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
>> I'm continuing to the chapter on "Deploying your Web Site" - Very excited!!
Excellent. Almost done. Congratulations.... ;-)
Imar
|
|
 |
|