Saturday, June 27, 2015

Uncaught Exception in Java: Yet Program Exits with Success?

In Java, you would have noticed that if we get an uncaught exception JVM exits with success (exit code is zero). While in C++ if there is an exception which is not handled, program returns with non-zero Exit Code. Only in case of explicit System.exit(), do we a non-zero Exit code, which reflects Failure. 

Some consider this a design issue, some consider this s defect. When such a code is called from within a script, coders tend to rely on exit status. They get confused often in Java as Success does not mean every time a successful exit.

What should we do in Java?
Write a top level try-catch in main. If any Exception is there, catch it and throw System.exit(1) explicitly. In an already written code that you are using, don't rely on exit codes.

6 comments:

  1. Nice blog. Really helpful information about Java …. Please keep update some more…………

    ReplyDelete
  2. Each department of CAD have specific programmes which, while completed could provide you with a recognisable qualification that could assist you get a job in anything design enterprise which you would really like.

    AutoCAD training in Noida

    AutoCAD training institute in Noida


    Best AutoCAD training institute in Noida

    ReplyDelete