开发者

Can't read the exception of try catch phrase

i put my code between try, catch like this

try
{
///
}
catch(Exception ex)
{
//here is where i set the break point
}

but when it enters the catch i can't read ex in quick watch wind开发者_运维百科ows as it says that it does not exist in the current context. Is this from the IDE itself?? because it happens with all the project i work on.


You will need to actually do something with the exception. I believe this has to do with some optimizations that the compiler\debugger does. Basically the compiler\debugger will see that the exception is no longer referenced\used and it won't be available. Do something like the following

try
{
///
}
catch(Exception ex)
{
   //here is where i set the break point
   Console.WriteLine(ex);
}


You are compiling in Release mode. In Release mode unused variables are deleted. Try in Debug mode or do something with the ex (for example log it somewhere, Console.WriteLine it, or do strange tricks that will probably confuse the compiler)

GC.KeepAlive(ex);

The compiler is cheated and it doesn't optimize away ex.


Is there any code in the catch block? Try putting a call to ex.ToString() in there and set the breakpoint on that. It may be that a compiler optimization is eliminating the block that doesn't do anything.


I get the same when I place a break point on the catch, but when I step into the brackets I can read the exception. Make sure you have code in the brackets:

        try
        {
            Convert.ToInt16("hoi");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.StackTrace);
        }


What Edition(std/pro/express) of Visual Studio Editor you are using too makes the difference? QuickWatch Dialog Box may not be available in all version of VS IDE. Please see the MSDN link below

http://msdn.microsoft.com/en-us/library/cyzbs7s2%28VS.80%29.aspx


The compiler was in the Debug mode but i figured out the reason, as some of you said it's all about the optimization of the code. Solution Explorer>Build> Uncheck "Optimize Code" checkbox. This is a temporary solution as I'll make a log for my application later. Thank you all for you help, I really appreciate it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜