本文介绍了如何在Visual Studio中进行调试时强制执行异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以调试模式运行我的应用程序,并且我想手动引发一个异常(即不是从代码内部).有什么办法吗?

I am running my application in debug mode, and I would like to manually throw an exception (i.e. not from within the code). Is there any way to do this?

当然,在命令"或立即"窗口中运行抛出新的Exception(我的强制异常"); 无效.

Of course, running throw new Exception("My forced exception"); in the Command or Immediate window doesn't work.

我希望异常由包围我正在调试的代码的try-catch语句捕获.

I want the exception to be caught by the try-catch statement that surrounds the code I'm debugging.

推荐答案

一种可行的方法是在执行以下操作之前,在一行上中断并手动将代码路径中的可为空的变量更改为 null .它发生.这将导致引发 NullReferenceException .

One possible way is to break on a line and manually change a nullable variable in the code path to null just before an operation on it occurs. This will cause a NullReferenceException to be thrown.

这篇关于如何在Visual Studio中进行调试时强制执行异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:41
查看更多