简体   繁体   English

我如何知道是否故意抛出了异常?

[英]How can i tell if an Exception was deliberately thrown?

I would like to catch an exception an tell if it was me that deliberately threw the error or something else like a runtime error (object not instance of an object). 我想捕获一个异常,以判断是否是我故意抛出了错误或其他类似运行时错误的错误(对象不是对象实例)。

try
{
    throw new Exception("throw this", "these can be many possible values");
}
catch (System.Exception ex)
{
    if (IThrew) // <--- how can i tell if i threw or not?
    {
        exReport = "one thing"; // <--- Should go in here in this example.
    }
    else
    {
        exReport = "another thing";
    }

    throw new FaultException<ExceptionReport>(exReport, new FaultReason(ex.Message), new FaultCode("Receiver"));

}

Clarification: 澄清:

I need keep record of all the exceptions then at the end display them in an exceptions report (array of exceptions). 我需要保留所有异常的记录,然后在异常报告(异常数组)中最后显示它们。 This is part of a schema I am REQUIRED to follow. 这是我必须遵循的架构的一部分。 (so please don't ask me to do it another way). (所以请不要让我以其他方式这样做)。

I have it all working great it outputs something like: 我的所有工作都很棒,它输出如下内容:

...
<soap:Detail>
<ows:ExceptionReport>
 <Exception exceptionCode="…" locator="…">
  <ExceptionText>…</ExceptionText>
 </Exception>
 <Exception exceptionCode="…" locator="…">
  <ExceptionText>…</ExceptionText>
 </Exception>
</ows:ExceptionReport>
</soap:Detail>
...

The problem is that when i'll have a few errors already in my ExceptionReport, then a runtime error will occur. 问题是,当我在ExceptionReport中已经有一些错误时,就会发生运行时错误。

But i've realized i'm going the wrong way about this... as Gary mentioned... i shouldn't be using exceptions as flow control. 但是我意识到我在这方面走错了路...正如加里所说的...我不应该将异常用作流控制。

Use a different exception. 使用其他异常。 Give it its own catch clause. 给它自己的catch子句。

Use a special type of exception for your own exceptions and check for that. 对您自己的异常使用特殊类型的异常并进行检查。

Otherwise you'd have to resort to inspecting stack traces to see if the origin is one of your assemblies, which is both ugly and unreliable. 否则,您将不得不检查堆栈跟踪以查看原点是否是您的程序集之一,这既丑陋又不可靠。

You can't tell why an exception was thrown. 您无法分辨为什么会引发异常。

What you can do is create your own exception classes so you can catch them - these will be the exceptions thrown on purpose. 可以做的是创建自己的异常类,以便您可以捕获它们-这些将是故意抛出的异常。 That is, since you created the exception class, the framework is not going to throw these. 也就是说,由于您创建了异常类,因此框架不会抛出这些异常类。

try
{
 // something
}
catch(MyCustomException ex)
{
   // Thrown by Application logic
}
catch(System.Exception ex)
{
   // Could by thrown by anything
}

You could look at the call stack on the exception and see if the top of it is your code - if so you directly produced the exception but there is no way to tell if you used a throw or, say, divided an int by zero 您可以查看异常上的调用堆栈,看看它的顶部是否是您的代码-如果是这样,则直接产生了异常,但是无法判断是否使用了throw或将int除以零

as the others have said the only sure fire way is to throw exceptions that could not be thrown by anyone else - ones that you control and have a common distinct base class 正如其他人所说,唯一肯定的解雇方法是抛出其他人无法抛出的异常-由您控制并具有共同的独特基类的异常

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

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