Is it possible that the Exception object throws another exception due to its internal error?

Is it possible that the Exception object throws another exception due to its internal error?

Suppose that in try-catch we create an instance of an Exception object, is it possible to instantiate another exception? If so, we should endlessly delay try-catch blocks that look so funny.

+5
source share
5 answers

In short, the answer is yes, it is possible.

For example, if an exception class requires a large object to be initialized as a field, but not enough memory to allocate it, you get an exception object that you would select OutOfMemoryException.

Exceptions are just like any other class and may themselves cause exceptions. There is nothing in the language that prohibits it.

, , - , , .


: ( )

try, catch ( , ). catch, try{}catch{} - , catch, .

- (, OutOfMemory StackOverflow), .

+3

- , .

( ) , , , .

, ? ?

, Reflector, , , -, .

+2

, , . , .

: , . , NullReferenceException.

+2

. .

, - ? , - , . , , , , catch.

+1

If you think that the exception object should try to handle the situation and throw another exception if it cannot be wrong.
The catch block should handle the wrong situation and throw an exception if it cannot do this.
Example:

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

   if (cannotHandle)
      throw e;
}
+1
source

All Articles