java - How to react to stack unwinding when destructors are not supported by a language? -
suppose have created instance of window
class. window shown user. then, exception thrown, , reference instance lost, window still seen user because instance still exists (it's not referenced anymore).
what in these circumstances?
i'm talking squirrel scripting language (http://www.squirrel-lang.org/). contrary java, doesn't seem have finally
blocks or finalizer methods, exception handling broken in language?
i don't know squirrel, in absence of block simulate behaviour extent within java:
exception error = null; try { // } catch (exception e) { error = e; } // code goes here // ... if (error != null) { // oh dear clean resources - files, windows, sockets etc. throw error; }
so catch block stores exception in variable can test later if want rethrow it, , still allows chance other cleanup. there nuances have aware of (e.g. explicit kinds of exception need special handling, more exceptions being thrown outside try / catch) careful consideration should okay.
system resources (like graphics handles, sockets, windows, file handles etc.) in particular tend bit messy in java , other garbage collected languages. these resources managed class explicit close() method. if know things have fallen in heap invoke explicit close() on objects clean them straightaway. otherwise object cleanup during finalization during gc long time coming.
Comments
Post a Comment