简体   繁体   English

确定代码是否抛出特定异常 c#

[英]Determine if code throws specific exception c#

Is is possible to know if a certain exception can be thrown inside a code block?是否可以知道是否可以在代码块内引发某个异常?

The premise: Assume you are working on some old codebase and you have something like this.前提:假设您正在处理一些旧代码库并且您有类似的东西。

try{
  a lot of code that calls a bunch of other methods
}
catch (CustomException e){
  some handling
}

I want to know what code in the try block that can throw this exception.我想知道 try 块中的哪些代码可以引发此异常。 For all I know nothing is throwing it anymore and this is just dead code.据我所知,没有什么是扔它了,这只是死代码。 I know this is caused by bad code design, but when working with real code it is not always possible to prioritize refactoring all of the old code.我知道这是由糟糕的代码设计引起的,但是在使用真实代码时,并不总是可以优先考虑重构所有旧代码。

So in short, is it possible to tell at compile time if anything in the try block actually throws that exception?简而言之,是否可以在编译时判断 try 块中的任何内容是否实际上引发了该异常?

(if it matters I am coding in idea rider) (如果重要的话,我在idea Rider中编码)

Edit: stackoverflow thinks this question already has an aswer to this question, but in reality that question does not have a good answer to anything.编辑:stackoverflow 认为这个问题已经回答了这个问题,但实际上这个问题对任何事情都没有很好的答案。 Question 问题

  • No there is no automatic technique不,没有自动技术
  • Yes there is a manual techniques: You look at the definition of the reference: for .net component you see in comment the input args, return and exceptions susceptible to trigger, but this is manual approach that must be done on all targeted methods.是的,有一种手动技术:您查看参考的定义:对于 .net 组件,您在注释中看到输入参数、返回和容易触发的异常,但这是必须在所有目标方法上执行的手动方法。

If your target code has undefined behavior, I suggest that you enhance your exception handling on time, and start with most generic exception in your first version.如果你的目标代码有未定义的行为,我建议你及时增强你的异常处理,并在你的第一个版本中从最通用的异常开始。 You log the content of exception analyse what is it then add this new type of exception in your error handling.您记录异常的内容,分析它是什么,然后在错误处理中添加这种新类型的异常。 in all the case if you let general exception in the end of catch you will catch everything and this is a current practice.在所有情况下,如果您在 catch 结束时允许一般异常,您将捕获所有内容,这是当前的做法。

While your answer describes a method to handle the situation, it is not really an answer to the question.虽然您的答案描述了一种处理这种情况的方法,但它并不是问题的真正答案。 – Fildor – 菲尔多

Yes it is an answer, do you know an automatic technique to list all Exceptions that can be triggered on a target code?是的,这是一个答案,您是否知道一种自动技术来列出可以在目标代码上触发的所有异常?

But in existing code it might be pretty dangerous to remove a catch block that handles a specific exception in a specific way.但是在现有代码中,删除以特定方式处理特定异常的 catch 块可能非常危险。 – Chippen – 奇彭

Where do I suggest tor Remove a catch block?我在哪里建议 to 删除一个 catch 块? If your target code already catches an exception, your caller code is not impacted by it as it will not see any exception, and there is no use to do this.如果您的目标代码已经捕获了一个异常,那么您的调用者代码就不会受到它的影响,因为它不会看到任何异常,并且这样做是没有用的。

If however you see explicitly in the target code that an exception is triggered with static code analysis, and if you want to see how to trigger it, you can design a unit test and create a scenario with special input to specifically trigger the code path, and to expect the exception in the test definition.但是,如果您在目标代码中明确看到使用 static 代码分析触发了异常,并且如果您想了解如何触发它,您可以设计一个单元测试并创建一个具有特殊输入的场景来专门触发代码路径,并期待测试定义中的异常。

In fact there is a problem of understanding between your question and my answer.实际上,您的问题和我的回答之间存在理解问题。 You want to ask.: For a given exception type, is there a way to know that a given target code can throw this exception.你想问:对于给定的异常类型,有没有办法知道给定的目标代码可以抛出这个异常。 Answer is you must analyse it manually or try to fuzz.答案是您必须手动分析它或尝试进行模糊测试。 My initial answer target another question which would be: for a given target code is it possible to know all exceptions that could be triggered.我最初的答案是针对另一个问题:对于给定的目标代码,是否有可能知道所有可能触发的异常。

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

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