简体   繁体   English

我希望C#表格崩溃而不是向我展示红十字会

[英]I want C# form to crash and not show me that Red Cross

I would like the form in C# to crash when something happens, like accessing an array/using a null variable. 我希望C#中的表单在发生某些事情时崩溃,比如访问数组/使用null变量。

I use C++ mainly, which usually crashes on the spot, so I know the problem immediately. 我主要使用C ++,它通常会在现场崩溃,所以我立刻就知道了问题。

Since I am to C#, I mainly write my stuff within the function, and the callstack simply reveals the OnPaint function being called. 因为我是C#,所以我主要在函数中编写我的东西,而callstack只是简单地显示了被调用的OnPaint函数。 So I've no idea where it is calling the exception. 所以我不知道它在哪里调用异常。

Couldn't find any solution and manually looking for the problem is really taking me too much time. 无法找到任何解决方案并手动寻找问题真的花了我太多时间。

Though it would be ideal for me to get these 2 solutions, either 1 would work. 虽然这对我来说是理想的,但是这两种解决方案都是可行的。

  1. Getting it to crash instead of throwing exception! 让它崩溃而不是抛出异常!
  2. Some way for the Callstack to show me where I am, it only shows OnPaint, where my OnPaint function has like many lines, thus taking me forever to debug it. Callstack的某些方式告诉我我在哪里,它只显示OnPaint,我的OnPaint函数有很多行,因此我将永远调试它。

ps I understand that I should have created separate functions to be called within, but I wasn't sure how to do it in C# initially when I just wanted to create this program quickly for its purpose. ps我明白我应该创建单独的函数来调用,但我不知道如何在C#中最初这样做,因为我只想快速创建这个程序用于它的目的。

Thank you in advance! 先感谢您!

Are you using Visual Studio? 你在使用Visual Studio吗?

If yes Go to Debug -> Exceptions and Check all the checkboxes in the dialog that opens 如果是,请转到“调试” - >“例外”并选中打开的对话框中的所有复选框

Now you would immediately break on error and at exact line... 现在你会立即在错误和确切线上打破......

Is there anything in the InnerException object on the Exception that's thrown? 在抛出的异常中的InnerException对象中是否有任何内容?

It might be worth be handling the exception and outputting the value of any variables that are in memory at that point into a log file of some sort. 可能值得处理异常并将该点内存中的任何变量的值输出到某种日志文件中。

Here's an example on how to do it: How to Log Exception in a file? 以下是如何执行此操作的示例: 如何在文件中记录异常?

如果您在开发环境中工作,请不要低估断点的功能。

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

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