answersLogoWhite

0

Sure you can, but it's never a good idea. You must never allow an exception to escape from a destructor. To do so would terminate the destructor prematurely, unwinding the call stack in search of an exception handler. This means that any remaining resources consumed by the instance, including its base classes, would be left in an invalid state with no possible way to recover those resources besides terminating the program. Any object that has the potential to throw an exception during its own destruction should always be treated with suspicion; it is a major design flaw.

User Avatar

Wiki User

10y ago

What else can I help you with?

Related Questions

What is issue in throwing an exception in a destructor?

If a destructor throws an exception, the instance is left in an invalid state. When an exception is thrown, the destructor automatically terminates at the point of the throw, unwinding the call stack until an exception handler is found (if one is provided). However, any resources yet to be released by the destructor, including all the instance's base classes, cannot be destroyed. When writing your own destructors, it is important to never throw an exception. If an exception could be thrown from within your destructor, you must catch it and handle it within the same destructor -- you must not rethrow the exception.


Is constructors throws exception in C plus plus?

We must throw an exception from constructor in C++, but remember that in case of exception in Ctor, destructor will not be executed and you may have memory leaks. SO make sure all the data members must clean up their mess own. e.g. use smart pointers so in case of excpetion memory will released to free storage.


How can you access the value throw an array?

You cannot throw an array, only exceptions can be thrown. You access the thrown exception by catching it in a catch clause of a try...catch statement.


How do you throw exception in java?

we do it using the throw keyword.


Can you throw exception from catch block in net?

Yes. Use C# code as an example: try { int k = 100 / 0; // this will throw an exception } catch (Exception e) { throw new SystemException("Throwing a new exception because of "+ e.Message); }


What is throw keyword in java?

The throw keyword is used from within a method to "throw" an exception to the calling method. In order to throw an exception, the method signature must indicate that it will throw an exception. For example: public void foo() throws Exception { } When you want to actually throw the exception, you can do it a few different ways: throw new Exception("Exception message"); or from within a catch block ( catch(Exception ex) ): throw ex;


Constructor cannot be virtual but destructor can be virtual justify?

bcoz constructor cant be invoked


What is throw exception in java?

The presence of the keywords "throws exception" on a method signature means that, the method may throw an exception whhich it does not handle. It also means that the method that is calling or invoking it has to handle such exceptions. If the calling method does not handle that exception it would have to in turn use the same "throws exception" clause and throw it to its parent method.


Does throws throws only unchecked exception?

You can throw any type of exception you want, including an unchecked exception.


How do you throw exception?

using throws class try, catch block we through the exception


If a method calls another method and that method throw an exception then caller method must be throw that exception or not?

If method A calls method B and method B throws an exception, then method A must handle that exception. It does not have to throw the exception if it is in a try-catch block, but it must do something to deal with it.Note that this only applies to checked exceptions. If method B throws an unchecked exception, then A is allowed to ignore it.


What is the difference between throw and throw new in net?

an exception object must follow the throw statement. That exception object must be created by new-operator initially. One can throw the same exception instance again without creating a new one (throw new) later. Just for fun and as an example: Let's on purposely cause 2 operations to throw exception. A third method will determine which operation produce the shorter exception message length, and throw the shorter one. public void Operation1() { throw new SystemException("From Operation 1"); } public void Operation2() { throw new SystemException("From Op2"); } public void GetShorter() { SystemException e1; SystemException e2; try { Operation1(); } catch (Exception e) { e1 = e; } try { Operation2(); } catch (Exception e) { e2 = e; } if (e1.Message.Length < e2.Message.Length) { throw e1; // an example of not using new-operator } else { throw e2; } }