Skip to Content
Beginning ASP.NET 3.5: In C# and VB
book

Beginning ASP.NET 3.5: In C# and VB

by Imar Spaanjaars
March 2008
Intermediate to advanced content levelIntermediate to advanced
766 pages
21h 15m
English
Wrox
Content preview from Beginning ASP.NET 3.5: In C# and VB

17.6. Practical Debugging Tips

The following list provides some practical tips to help you debug your application.

  • Never leave debug="true" in the web.config file for the site in a production environment. Always set it to false to improve performance and ensure solid operation of your site. In Chapter 18 you see an even better solution to ensure this setting is never set to true on a production server.

  • Try to avoid swallowing exceptions in a Catch block. You may be tempted to wrap your code in a Try/Catch block and then leave the entire Catch block empty. Although this certainly avoids exceptions showing up in the user interface, it makes debugging extremely difficult. Because you are no longer aware a problem occurs, you also cannot write code to prevent the error from happening in the first place. The general rule here is: Catch errors that you can handle successfully, for example by displaying a message to the user. If you can't handle the exception in your code, let it bubble up and log it in the Application_Error event handler so you know that the exception occurred.

  • If you need to re-throw an exception in a Catch block, don't use Throw ex (throw ex in C#), but use Throw (throw in C#) only. When you use Throw ex, you make it difficult to track the path the code has followed before the exception occurred. Here's the code:

    VB.NET

    Try ... Catch ex As Exception Throw ex ' Bad example; you lose track of the source of the exception Throw ' Good example; forwards the exception and maintains ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning ASP.NET 4: in C# and VB

Beginning ASP.NET 4: in C# and VB

Imar Spaanjaars
Professional ASP.NET 3.5 SP1 Edition: In C# and VB

Professional ASP.NET 3.5 SP1 Edition: In C# and VB

Bill Evjen, Scott Hanselman, Devin Rader

Publisher Resources

ISBN: 9780470187593Purchase book