Tuesday, October 07, 2008

Debug.WriteLine to the rescue of 'e is not declared but not used ...'

Debug.WriteLine to the rescue of 'e is not declared but not used ...'

While using C# as our prime programming language and with try catch statement, we often end up having a compiler warning like the exception variable is declared but not used. Having a compiler warning is like dangerous since we saw sometime back that we should treat compiler warnings as errors as a good programming practice. But in this case, to handle this elegantly, we can make use of System.Diagnostics namespace which has a class called Debug

try
{
//Your code block ...
}
catch (ApplicationException ee)
{
Debug.WriteLine (ee.Message);
}

By default, Debug.WriteLine is wired to your output window of Visual Studio IDE and hence this would also be easing your debugging effort. I hear that a few would be thinking that we can use catch without the parenthesis. I strongly discourage this practice since during debugging and if you are to examine the exception, this becomes a little trickier.

No comments:

[Imported from Blogdrive]Online Virus Scanners

Online Virus Scanners Virus Scanners are no longer difficult to install, tedious to configure. There are easy to use Online Virus Scanne...