有没有办法在 C# 中继续异常?

发布于 2024-08-15 05:07:47 字数 161 浏览 9 评论 0 原文

当您的程序(在调试器中)发生意外异常时。有时您只想跳过它,因为此时终止程序比继续运行更有害。或者您只是想继续,因为您对另一个错误/错误更感兴趣

是否有选项/编译器标志/秘密开关来启用此功能?

我知道应该立即解决异常情况,但在某些情况下(就像我所描述的那样),人们只想暂时跳过它

When an unexpected exception occurs in your program (in the debugger). Sometimes you just want to skip it since killing the program at that point is more harmful than continuing. Or you just want to continue since you were more interested in another error/bug

Is there an option/compilerflag/secretswitch to enable this?

I understand exceptions should be resolved right away, but there are scenarios (like I described) where one just wants to skip it for the time-being

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

待天淡蓝洁白时 2024-08-22 05:07:47

如果代码中没有适当的 catch 块,您就无法做到这一点,不。但是,我不记得曾经想要这样做:如果发生异常并且您的代码不知道如何真正处理,那么您为什么要继续?那时你的状态很糟糕——继续下去会很危险。

您能否举例说明为什么您希望在调试器会话中继续而不是在生产代码中继续?

You can't do this without an appropriate catch block in your code, no. However, I can't remember ever wanting to do this: if an exception occurs which your code doesn't know how to genuinely handle, why would you want to continue? You're in a bad state at that point - continuing would be dangerous.

Can you give an example of why you'd want to continue in a debugger session but not in production code?

ら栖息 2024-08-22 05:07:47

C# 中的异常是不可恢复的,但事件是可恢复的 - 这就是可恢复异常的典型实现方式:如 可取消的事件。另请参阅此问题

Exceptions in C# are not resumable, but events are - and that is how resumable exceptions are typically implemented: as cancellable events. See also this question.

走走停停 2024-08-22 05:07:47

使用 try-catch 块,捕获时不对异常执行任何操作。

Use a try-catch block, and when catching, don't do anything about the exception.

反话 2024-08-22 05:07:47

如果您进入调试器,请右键单击要继续的行并选择:设置下一条语句...但使用它需要您自担风险!

If you are into the debugger then right click on the line you want to continue and select: Set Next Statement... but use it at your own risk!

初见你 2024-08-22 05:07:47

在调试模式下单步执行代码时,您可以跳过引发不需要的异常的指令的执行。但如果异常已经抛出并且你没有 try/catch ,它就会传播。

When stepping through the code in debug mode you could skip the execution of the instructions that throw the undesired exception. But if the exception is already thrown and you don't have a try/catch it will propagate.

梦途 2024-08-22 05:07:47

请查看异常处理应用程序块和相关文档。它包含处理应用程序异常的最佳实践,并且有很多为您完成的框架代码,即日志记录。

Have a look at the Exception Handling Application Block and related documentation. It contains best practices for handling application exceptions and there is a lot of framework code done for you i.e. logging.

暖树树初阳… 2024-08-22 05:07:47

如果你想知道你想允许什么例外。那么你可以在下面执行此操作

try
{
       // your functionality
}
catch(Exception ex)
{
     // Catch only the exceptions you need to point out
}
finally
{
   //do what you want to complete with this function.
}

If you want to know what exception you want to allow. then you can do this below

try
{
       // your functionality
}
catch(Exception ex)
{
     // Catch only the exceptions you need to point out
}
finally
{
   //do what you want to complete with this function.
}
饭团 2024-08-22 05:07:47

我认为“跳过”意味着您希望程序在异常发生后继续工作。
当然,这可以通过使用 try-catch 块捕获异常来实现。

如果异常不是应用程序停止程序(例如,某些关键变量在异常后未初始化,并且您无法继续工作),建议您至少在继续之前将其记录下来。当然,放

捕获(异常 e){ }

在源代码中的任何地方都不会导致稳定的应用程序;)

如果您的问题与调试器更相关(您不希望调试器在每个抛出的异常上停止) ,那么 VS 中有一个地方可以更改此设置:

调试菜单中,选择异常。您将看到所有可能的异常,并且可以在用户抛出或未处理时调整它们的行为。

I assume that by "skipping" you mean that you want your program to continue working after the exception.
That, of course, is possible by catching the exception when using try-catch block.

If the exception is not application stopper (for example, some key variable is not initialized after the exception, and you can't continue work) it is recommended that you at least log it before continue. Of course, putting

catch (Exception e) { }

everywhere in your source will not lead to a stable application ;)

If your problem is more debugger-related (you don't want the debugger to stop on every thrown exception), then there is a place in VS where you can change this:

In the Debug menu, select Exceptions. You'll see all possible exceptions and you can adjust their behaviour when thrown or not handled by the user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文