• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Why is it so that in java we cant catch System.exit(1) whereas in Python we can catch system.exit(1)

 
Ranch Foreman
Posts: 2891
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java as soon as System.exit(1) comes, it exits the program and does not go in the catch block. Whereas in python we can catch system.exit(1). Why is it that in Java it cannot be caught whereas in python it can be caught using a try except block? thanks
 
Saloon Keeper
Posts: 10555
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java the use of System.exit() is discouraged because open resources may not be closed properly. Exit() means "I  don't care what you're in the middle of, stop and leave the program". Catch on the other hand says "ok, I got this and I'll  do something meaningful with it and then continue", which is exactly the opposite. Why wouldn't you just throw and exception if you want to catch it?
 
Monica Shiralkar
Ranch Foreman
Posts: 2891
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:In Java the use of System.exit() is discouraged because open resources may not be closed properly. Exit() means "I  don't care what you're in the middle of, stop and leave the program". Catch on the other hand says "ok, I got this and I'll  do something meaningful with it and then continue", which is exactly the opposite. Why wouldn't you just throw and exception if you want to catch it?



I mean the case where one program calls another program and the second program give exit(1) and we have to catch it.
 
Marshal
Posts: 28009
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's true that you can't use a try-catch block to catch system termination. But it is nevertheless possible to write code which runs when the program is shutting down. That's called a "shutdown hook" and you can set that code up to be ready for shutdown by using the Runtime.addShutdownHook(Thread) method.

I don't believe it's possible to determine, inside the shutdown hook, why the program is shutting down. And of course the shutdown hook may not work reliably in some cases (e.g. when a large rock crushes the computer). So I don't think it can tell System.exit(1) from System.exit(22) or from the large rock.
 
Paul Clapham
Marshal
Posts: 28009
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:I mean the case where one program calls another program and the second program give exit(1) and we have to catch it.



Is that one Java program calling another Java program? How did you do that?
 
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will a shutdown hook run when System.exit() is called?
 
Carey Brown
Saloon Keeper
Posts: 10555
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:I mean the case where one program calls another program and the second program give exit(1) and we have to catch it.


I haven't tried this but I believe something like this should work.
 
Monica Shiralkar
Ranch Foreman
Posts: 2891
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When anyway Java has a shut down hook mechanism through which one can do this then what is the reason that it was not made so that Catch block could catch System.exit(1) ?
 
Paul Clapham
Marshal
Posts: 28009
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Will a shutdown hook run when System.exit() is called?



Yes.
 
Paul Clapham
Marshal
Posts: 28009
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:When anyway Java has a shut down hook mechanism through which one can do this then what is the reason that it was not made so that Catch block could catch System.exit(1) ?



A try/catch block is there so it can catch an exception. System.exit() does not throw an exception, so.

And remember what the purpose of System.exit is. The purpose is to end the program. So making it possible for something else in the code to intercept that, so that the program does not actually end, would be an incoherent design.

Besides, a command to exit out of a program with a return code already existed in other languages when Java was designed. So Java got it too, and why would it be different when it's a standard thing to do?
 
Carey Brown
Saloon Keeper
Posts: 10555
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Campbell Ritchie wrote:Will a shutdown hook run when System.exit() is called?



Yes.


To be clear, if the shutdown hook is established in the java program calling exec() of another java program, and that other program calls exit(1), then NO the hook will not be invoked because the calling java program is not exiting, only the called program is.
 
Carey Brown
Saloon Keeper
Posts: 10555
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't been crystal clear on this but I'm sensing that if the CALLED program does an exit(1) that you expect that the CALLING program will throw an exception. Yes? No?

... and the CALLING program is supposed to exit(1) also?
 
Monica Shiralkar
Ranch Foreman
Posts: 2891
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

And remember what the purpose of System.exit is. The purpose is to end the program. So making it possible for something else in the code to intercept that, so that the program does not actually end, would be an incoherent design.


Yes. And I saw that in Python it allowed to catch system.exit(1). What may be the reason that it is allowed there ?
 
Monica Shiralkar
Ranch Foreman
Posts: 2891
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You haven't been crystal clear on this but I'm sensing that if the CALLED program does an exit(1) that you expect that the CALLING program will throw an exception. Yes? No?

...



Yes calling program should throw and get exception but that it can do only once it is able to catch the System.exit.

and the CALLING program is supposed to exit(1) also?



The calling programs will log it and rethrow it
 
Saloon Keeper
Posts: 27494
195
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short answer. System.exit() is not an Exception and does not throw any Exceptions (or Throwables of any kind). Thus you cannot "catch" it.

On the other hand, as has been explained, you can attach shutdown hooks, which gives the same net effect.

In OepnJDK, System,exit() invokes Runnable.exit(), which invokes Shutdown.exit(). The Shutdown.exit() method not only runs the shutdown hooks, it also runs any pending finalizers. Then it invokes halt() to actually terminate.

One thing I can't tell from the source is what happens if an uncaught Exception bubbles its way up out of the main() method without stopping since we're not longer in the application when that happens. One option would be to ignore the hooks and finalizers. Another would be if the code that invokes main() is simplemented in the following pseudo-java:
 
Carey Brown
Saloon Keeper
Posts: 10555
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Based on your clarification you should use this.

If you want  to use a  shutdown hook also you can but that is for handling shutdowns not the ExitException that you say you want.
 
Paul Clapham
Marshal
Posts: 28009
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I notice that the Process class has an exitValue() method; it returns an int value which I suspect would be the "1" in Monica's example.
 
Carey Brown
Saloon Keeper
Posts: 10555
83
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:I notice that the Process class has an exitValue() method; it returns an int value which I suspect would be the "1" in Monica's example.

waitFor() returns the same value.
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic