Is a finally block executed when an exception is thrown from a try block that does not have a catch block and if so when?
By Paul King, OCI Senior Software Engineer Show May 2000 The mechanics of the finally blockFor Java developers, the try-catch block is a way of life. A fundamental part of this mechanism is the finally block. As the name suggests, code in a finally block is always executed last. A finally can appear optionally at the end of a try-catch block as shown:
Code in a finally block runs immediately after one of the following three things occurs:
So, finally will be executed once if no exceptions occur in the try block, or if multiple exceptions occur in the try-catch blocks. This article focuses on the technical details of the finally block, and situations where it can be put to good use. Note: The try-catch paradigm exists in C++, but that language does not support finally. The C++ destructor can serve a similar purpose as the finally block in Java; however, for those of you who know C++, it should be clear that they are quite different animals. When to use finallyfinally blocks are useful when you want to make sure that particular processing always happens prior to completing a method or a block of code in a method. To accomplish this in many other languages, multiple lines of redundant code are often necessary. Such redundant code can be prevalent when attempting to handle errors and can obscure the fundamentals of the processing. They are a ripe source of programming errors. Error handling often involves freeing or releasing shared resources, such as sockets, CORBA objects, database connections, and files, and performing other clean-up operations. The example below demonstrates how finally can be used to ensure that a socket obtained from a group (or pool) of sockets is released upon completion of a transaction. By placing the release of the socket in the finally block, we are guaranteed that the resource will be freed for subsequent use. Even if an exception that we do not handle occurs in the try block, or if an exception is thrown while executing a catch block, the release will still occur. The finally block provides a convenient means to buttress the try-catch with a defensive block to perform clean-up that needs to occur, whether we complete the transaction normally or fail as a result of an error. We also include logging a diagnostic message in our finally block, since it is a convenient place to update an audit-trail. Note: For the sake of brevity, the definition of the class that implements the socket pool (the pool member variable shown below), a member of the pool (the PooledSocket class shown below), and a file-logging utility (the log member shown below) are not included with this example. These classes may be included in a future article.
ComplicationsOne must be careful if completion of a try-catch block occurs as a result of executing a return. If a finally block also returns a value, then that return supercedes any previous return in the try-catch block. Also, if an exception was thrown in the try or catch blocks that was not caught, then execution of a return in the finally block prevents the exception from being thrown to caller (because it is not possible for the caller to simultaneously evaluate the return and catch the exception). Similarly, if an exception was thrown in the try-catch block, an exception thrown in the finally block will supercede the prior exception. Due to these complications, it is probably a good idea to avoid returning a value or purposely throwing an exception in a finally block. SummaryWhen implementing a try-catch block, consider the following:
Software Engineering Tech Trends (SETT) is a regular publication featuring emerging trends in software engineering. More Popular ArticlesWhen finally block is executed?The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.
Will finally execute after exception?Yes, the finally block will be executed even after a return statement in a method. The finally block will always execute even an exception occurred or not in Java. If we call the System.
When finally block is not executed?A finally block will not execute due to other conditions like when JVM runs out of memory when our java process is killed forcefully from task manager or console when our machine shuts down due to power failure and deadlock condition in our try block.
Is finally block executed without try?No. Finally cannot be used without a try block. The try block defines which lines of code will be followed by the finally code. If an exception is thrown prior to the try block, the finally code will not execute.
|