is there a way to continue an exception in C#?

2019-05-04 01:56发布

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

8条回答
何必那么认真
2楼-- · 2019-05-04 02:07

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.

查看更多
地球回转人心会变
3楼-- · 2019-05-04 02:11

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.

查看更多
手持菜刀,她持情操
4楼-- · 2019-05-04 02:11

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!

查看更多
乱世女痞
5楼-- · 2019-05-04 02:15

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.
}
查看更多
Root(大扎)
6楼-- · 2019-05-04 02:21

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?

查看更多
【Aperson】
7楼-- · 2019-05-04 02:23

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

查看更多
登录 后发表回答