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 ...