Break at throw for exception that is caught

僤鯓⒐⒋嵵緔 提交于 2019-12-10 22:20:01

问题


In the VS debugger, un-caught exceptions result in the program breaking at the point the exception is throw (or near enough) and in a state that lets you look at all the stack frames and there local variables up to that point.

Is there a way to get this same result (break at throw) but for an exception is caught at a particular point? I'm not interested in doing this for ALL exceptions or even all exception of a given type (that could get useless real quick) but if I could do it for a single try or catch block I'd be happy

somewhat related:

  • How to work around ‘Break when thrown’ (Is per class and I'm looking for per try/catch block)

回答1:


Does the Debug -> Exceptions dialog do what you want? You can select which Exceptions that will cause VS to break, regardless of whether they are caught or not. I don't know of a way to do this for only a certain part of the code, only based on the type of the exception thrown.




回答2:


Yes, you should be able to put a breakpoint on the last brace of your catch block. Or the throw command if you're re-throwing.

If you just need to have a breakpoint on any exception inside of a certain method do a re-throw.

try {  }
catch (Exception exc)
{ 
   throw;  // <-- breakpoint here
} 

Edit: I was once in the habit of putting breakpoints on just about all of my exceptions. Found out the hard way that this slowed down the debugger in a big way once I got to about 25 breakpoints. May only be relevant to VS2005.

Edit2: The location that caused the exception ought to be in the exc object's StackTrace.



来源:https://stackoverflow.com/questions/418613/break-at-throw-for-exception-that-is-caught

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!