简体   繁体   English

无法读取try catch短语的异常

[英]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 windows as it says that it does not exist in the current context. 但是当它进入到捕获状态时,我无法在快速监视窗口中读取ex,因为它说它在当前上下文中不存在。 Is this from the IDE itself?? 这是来自IDE本身吗? 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) 尝试在Debug模式下进行调试,或使用ex做一些事情(例如,将日志记录在某个位置,Console.WriteLine进行记录,或者执行一些可能使编译器感到困惑的奇怪技巧)

GC.KeepAlive(ex);

The compiler is cheated and it doesn't optimize away ex. 编译器被骗了,并且无法优化ex。

Is there any code in the catch block? catch块中是否有任何代码? Try putting a call to ex.ToString() in there and set the breakpoint on that. 尝试在其中调用ex.ToString()并在其上设置断点。 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? 您使用的Visual Studio Editor的哪个版本(std / pro / express)也有所不同? QuickWatch Dialog Box may not be available in all version of VS IDE. 并非在所有版本的VS IDE中都提供“快速监视”对话框。 Please see the MSDN link below 请查看下面的MSDN链接

http://msdn.microsoft.com/en-us/library/cyzbs7s2%28VS.80%29.aspx http://msdn.microsoft.com/zh-CN/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. 谢谢大家的帮助,我非常感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM