Aries512's blog

By Aries512, 13 years ago, In English

Sometimes it is useful to terminate program immidiately, and the easiest way to do so in Java is via System.exit(0). However, it is rather poor programming practice and on Java courses I took, we were heavily discouraged to use it, as it terminates the whole currently running JVM and this can cause trouble.

So I want to ask, if it is ok on Codeforces. I already tried in one of my submission and it worked (720582), but I want to make sure it is ok in every situation, nobody likes to lose points during contest because of some unexpected error.

  • Vote: I like it
  • +14
  • Vote: I do not like it

13 years ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

Any programming courses may tell you a lot of things which are regarded as "bad programming practice" and which may appear very handy in real word programming.

On the other hand there are a lot of "bad practices" about which you will never hear during any courses.

What about "System.exit(0)" it is surely all right in the small programs which we write here - if we are solving some problem trying a lot of variants and one of variants succeeds - and we get it in third-level nested function - surely we just do not want program to continue - and we do not want mess up with complicated conditions which allow us to get out of all these nested subroutines.

If you are writing some very big project with several colleagues - surely they would not be glad if you insert some hidden System.exit() to halt your application abruptly. However, you yourself will never have such a fancy if you are sane enough programmer. In case of some abnormal error you would just "throw new Error()" so anyone could find the reason in a few seconds (since we get full stack trace with it).