Helo! For exam purposes it is required to know the effect to final block of call to System.exit(0) I have, although, other questions in mind: 1) are the finalizers run? 2) how can one "catch" System.exit(0)? 3) Should use of System.exit(x) be banned?
------------------ Antti Barck It Solutions Consultant -- NSD Oy Sun Certified Programmer for the Java™ 2 Platform
As I understand it, the finalizers will not be run with a call to System.exit(). System inherits directly from Object, not Throwable, so you cannot "catch" a call - it's questionable as to whether it would make sense to anyway. I personally find System.exit() handy, although with good design you can limit it's use if you want to. HTH Adam
Antti.. Has some Good questions For the Q1: From the Java Spec: protected void finalize() throws Throwable Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup. public static void exit(int status) Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination. In my humble opinion Finalizer cleans up the resources and objects,i dont think it can call system.exit() Any comments from fellow Ranchers? Thankx
There is nothing wrong with System.exit(). By default finalizers are not run, however there is System.runFinalizersOnExit() if you want that (be warned that it is a deprecated method, but I doubt it will ever be removed). There are also shutdown hooks (they can catch interrupts too!). You shouldn't use a shutdown hook to catch and resume after an attempt exit, but you could do some cleanup. You can disable System.exit() by using a custom SecurityManager. In that case, calling it would throw a SecurityException and you could catch that.
Helo David! Thx for your thoughts. I think I will have to re-design the software which lead me pondering the System.exit() call at first place. ------------------ Antti Barck It Solutions Consultant -- NSD Oy Sun Certified Programmer for the Java™ 2 Platform