If You Put System.exit(0) on Try or Catch block, Will Finally Block Execute?

Perhaps irrationally, the concept of computer programming (and coding, more broadly) is, to many, intimidating or, at a minimum, alienating. Mere mention of programming as a profession seems to tacitly suggest inaccessible levels of labyrinthine knowledge and intellectual capacity the likes of which is beyond average human beings. And to some extent, this is understandable. Without at least a cursory understanding of programming tenets and basic rules, the work of shaping software via endless lines of code must appear to laypeople like sheer sorcery.

There has always been power in the esoteric, and jargon tends to overwhelm the uninitiated. This is true of virtually any field. Try being the only non-M.D. at a dinner party – you will soon feel as though you’ve entered a parallel universe, one in which a language once familiar to your ear has been distorted beyond recognition. Coding is no different. It is replete with its own terminology, shorthand, and phrases, all of which exist to render the programming industry more internally productive, even at the expense of rendering it utterly mysterious to outsiders

Advanced Language, Decoded

A major contributing factor is coding’s highly logic-driven and supremely systematic nature. There is simply little (if any) room for error. Advanced software and even the most pedestrian of applications must be governed by tight rules and procedures to function as intended. A seemingly minor mistake in the framing of a coding passage can compromise efficacy throughout the program it is intended to support. For that reason, programming languages and platforms tend to guide their users into structured ways of thinking, thereby mitigating the risk of coding errors. Every step along the way is outfitted with safeguards and opportunities to double-check existing work while correctly framing the work to come.

A Broader Programming Appeal

Many programming languages are necessarily numerical in their construction, which places them further beyond the reach of non-professional or beginning programmers. Java was introduced over twenty years past with the aim of opening the art and science of programming to a larger population segment. Its interface and functionality are, much like this article, predicated upon the English language. It might be argued that Java serves as a C++ translator. It is characterized by a lesser degree of complexity than C++ but succeeds in providing coders with a powerful conduit through which to realize their software/application programming needs or wants. It does this without compromising its programming value, as Java’s application programming interfaces are famously well-designed and accessible even to fledgling programmers.

What are Java Blocks?

Anyone who plans on engaging with Java’s programming language will need to acquaint themselves with the block. Put simply, a block within the Java realm is a standalone statement enclosed by so-called curly braces, or these: {}. Exception handlers are referred to as try blocks. These are essentially safeguards to ensure a program will continue performing even if an exception to the basic statement occurs. Following the try block is at least one (though often several) catch blocks, which confront and, as expected, catch the exceptions, preventing them from disrupting program functionality. A finally block is in place to handle any exceptions not accounted for in the catch block. It essentially functions as a failsafe coding measure.

The System.exit(0) Scenario and Block Considerations

Java allows for a termination of system operations (of its Virtual Machine) via the System.exit command, or system call. By entering “0” in the command, the termination is identifiable as having been normal. The question remains, how does the System.exit(0) system call affect the extant finally block? This is integral information for any Java user, as block functionality is a mainstay of programming within that language. There is tremendous value in correctly understanding the capabilities and limitations of each block and the sequencing to which they are subject.

Discerning Java’s Default Order of Operations

Put simply, a System.exit(0) system call prevents the finally block from executing if no “catch” is encountered en route to the exit. Both the catch and finally blocks are essentially bypassed, as the system is established as the final statement to be executed, after which nothing will follow. If the Java System.exit(0) call does run across a “catch,” the finally statement will precede the Java “end program” process in its execution. In other words, the system needs to be prompted for the finally block to engage. If the exit is otherwise normal, finally will fail to execute.

This question comes down to sequencing. If the blocks are in place to catch identifiable exceptions, one or more of those exceptions might prompt the finally block to execute prior to a normal exit from the Java VM system exit. But barring the system “catching” a prescribed exception, the finally block will be as readily bypassed as the others. If it is of relevance to your own Java usage, implement logical exceptions (security and otherwise) to ensure the finally block is executed to your liking.

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.