A good programmer is a lazy programmer, because he writes minimum code.

Thursday, December 31, 2009

Break when an exception

The scenario is like this. You are debugging some code. From the code an exception is thrown. As the exception handlers are in place, you know the exception happened only when the flow reaches exception handler code. So, in order to find out the exact line of code, where the exception happened originally, you need to step through each line. While doing this, if you lose your concentration for a second, alas, it jumped to exception handler. Is there a solution for this?

I am surprised to find out that only very few people are aware of this powerful feature, even though it was available from old vb6 days. Microsoft should do something to popularize this. In Visual Studio, you can set to break the execution the moment exception happens, in the exact line where exception happened. Nice, no? Here are the steps.
  1. In Visual Studio, when a project is open, click on Debug menu, click on Exceptions. (or press Ctrl+Alt+E)
  2. Check the check box below Thrown column, against Common Language Runtime Exceptions.
  3. Run the code again, and watch the code is crashing at line where the error occurred.
  4. Once the code is crashed, if you want to find out how it is handled by exception handler, press F11 (Step Into).

You can even expand Common Language Runtime Exceptions, and configure for individual exceptions. But I don’t think anyone is going to use this.

You can set this option any time, even in the middle of debugging. There is no need to stop debug and restart.

Remember, this option has importance only when debugging. Don't worry that setting this option will cause your program to crash at production.

I am sure once you find this feature, it becomes a cannot-live-without stuff.

Happy new year.

No comments:

Post a Comment