How to react to stack expansion when destructors are not supported by language?

Suppose you created an instance of a class Window. The window is displayed to the user. Then an exception is thrown and the reference to the instance is lost, but the window is still visible to the user because the instance still exists (it is no longer mentioned).

What to do in these circumstances?

I specifically talk about the Squirrel scripting language (http://www.squirrel-lang.org/). Unlike Java, it has no blocks finallyor finalizer methods, so exception handling doesn't work in this language?

+3
source share
1 answer

, finally Java:

Exception error = null;
try {
 // do something
}
catch (Exception e) {
  error = e;
}
// My finally code goes here
// ...
if (error != null) {
  // Oh dear clean up all my resources - files, windows, sockets etc.
  throw error;
}

, catch , , , - . , , (, , , try/catch), .

(, , , , ..), , , Java . close(). , , , () , . , GC, .

0

Source: https://habr.com/ru/post/1768385/


All Articles