• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

System.exit(x)

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic