简体   繁体   English

由于内部错误,Exception对象是否可能引发另一个异常?

[英]Is it possible an Exception object raising another exception due to its internal error?

Is it possible an Exception object raising another exception due to its internal error? 由于内部错误,Exception对象是否可能引发另一个异常?

Assume in try-catch we instantiate Exception object, is it possible the instantiation raising another exception? 假设在try-catch中我们实例化Exception对象,是否有可能实例化引发另一个异常? If yes, we must nest infinitely try-catch blocks that looks so funny. 如果是的话,我们必须无限地嵌套看起来很有趣的try-catch块。

In short, the answer is yes, it is possible. 简而言之,答案是肯定的,这是可能的。

For example - if the exception class requires a large object to be initialized as a field, but there is not enough memory to allocate it, you will get an exception object that would throw an OutOfMemoryException . 例如 - 如果异常类需要将一个大对象初始化为一个字段,但没有足够的内存来分配它,你将得到一个抛出OutOfMemoryException的异常对象。

Exceptions are like any other class and can in themselves throw exceptions. 异常就像任何其他类一样,并且可以自己抛出异常。 There is nothing in the language that disallows it. 语言中没有任何东西不允许它。

I would say, however, that throwing exceptions from an exception class it bad practice and should generally be avoided. 但是,我会说,从异常类中抛出异常是不好的做法,通常应该避免。


Update: (following updated question) 更新:(更新后的问题)

If you are instantiating an exception object in a try block, the catch will catch it (assuming it catches the appropriate type of exception). 如果要在try块中实例化异常对象, catch将捕获它(假设它捕获了适当类型的异常)。 If you are instantiating it in the catch block, you may want to do that in a nested try{}catch{} - this is quite normal for code used in a catch block that can throw exceptions. 如果您在catch块中实例化它,您可能希望在嵌套的try{}catch{}此操作 - 对于可能抛出异常的catch块中使用的代码,这是很正常的。

As others have said - some exceptions should not be caught (for instance OutOfMemory or unexpected StackOverflow ), as you don't have a way to deal with them. 正如其他人所说的那样 - 不应该捕获一些异常(例如OutOfMemory或意外的StackOverflow ),因为你没有办法处理它们。

Yes - certainly it is possible for an exception itself to raise an exception. 是的 - 当然,异常本身可能会引发异常。

However, most (not all) of the framework exceptions are pretty lightweight things with very little internal logic, so the average exception probably doesn't have a lot of scope to generate exceptions itself. 但是, 大多数 (并非所有)框架异常都是非常轻量级的东西,内部逻辑很少,因此平均异常可能没有很多范围来自己生成异常。

Is this a framework exception you are seeing this behaviour with? 这是一个框架异常,你看到这种行为吗? Can you give us some details? 你能告诉我们一些细节吗?

A quick look at the internals of the exception with a tool like Reflector may well help you spot what, if anything, is going on. 使用像Reflector这样的工具快速查看异常的内部结构可能会帮助您发现正在发生的事情(如果有的话)。

Yes, it's possible although not very typical. 是的,这可能虽然不是很典型。 If the exception being thrown has an error in the constructor, or depends upon missing classes, then it itself throw an exception. 如果抛出的异常在构造函数中有错误,或者依赖于缺少的类,那么它本身会抛出异常。

It's easy to test this: create your own exception that attempts to call a method an null reference. 测试这个很容易:创建自己的异常,尝试将方法调用为null引用。 When you instantiate the exception, it will throw a NullReferenceException. 实例化异常时,它将抛出NullReferenceException。

Yes . 是的 This is possible. 这个有可能。

But I wonder why one would ever want to do this ? 但我想知道为什么人们会想要这样做? Exceptions are just for communicating errors so practically by design, they cannot have any serious code or logic running within them that can cause an exception. 例外仅仅是为了通过设计实际传达错误,它们不能在其中运行任何可能导致异常的严重代码或逻辑。 If you're argument is, you might want to log the exception into database or disk which might cause an exception in some conditions, then I'd not even agree to that since the exception should be logged in the catch block. 如果您的参数是,您可能希望将异常记录到数据库或磁盘中,这可能会在某些情况下导致异常,那么我甚至不同意这一点,因为异常应该记录在catch块中。

If you are thinking that the exception object should attempt handle the situation and throw another exception if it couldn't you are incorrect. 如果您认为异常对象应该尝试处理该情况并抛出另一个异常,如果它不可能你是不正确的。
The catch block should handle the incorrect situation and throw the exception further away if it can't do so. catch块应该处理不正确的情况,如果不能这样做,则将异常抛出更远。
Example: 例:

try
{
   BigResource r = new BigResource();
}
catch(BigResourceException e)
{
   bool cannotHandle = false;
   // Handle exception here

   if (cannotHandle)
      throw e;
}

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

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