answersLogoWhite

0

Theoretically, the finally block will execute no matter what. But practically, the following scenarios can prevent the execution of the finally block.

* The death of thread

* Use of system.exit()

* Turning off the power to CPU

* An exception arising in the finally block itself

User Avatar

Wiki User

13y ago

What else can I help you with?

Continue Learning about Engineering

What is finally block in java?

The Finally block in java is used along with the try-catch statements. The usual structure is try { ..... } catch(Exception e){ .... } finally { ..... } In the finally block we usually have code that is used to perform clean up activities corresponding to the code in the try block. When an exception occurs in the try block control comes to the catch block and the rest of the code in the try block would not execute. In such cases we may need to have some code that cleans up the objects that we created/used in the try block. No matter what happens, the code inside the finally block would definitely execute. The best use of finally block is when we are connecting to the database. In the try block we would have the statements that instantiate the connection, prepared statement, result set etc. If the query fails then all these objects would be left open and the code would continue. So in the finally block we usually nullify these statements so that even in case of an error the unused objects are cleared. Example: Connection con; PreparedStatment PS; ResultSet rs; try{ con = ConnectionManager.getConnection(); PS = con.prepareStatement(query); rs = PS.executyQuery(); ...... } catch (Exception e){ e.printStackTrace(); } finally { con.close(); PS.close(); } In case of an error the connection & prepared statement objects would remain open if we do not have the finally block. Tip: Whatever code we have inside the finally block would be run definitely if the try block is invoked. We can even use the finally block to check if the try block was reached and executed. If the try executes then we can rest assured that the finally would also get executed.


How do you stop execution of a program at a certain line?

Enable tracing


What is interrupt and why used interrupt?

interrupt means to stop the execution of some task and save it, start the new task which is caused by it. interrupt increases the efficiency of a computer and caused by multi-tasking. mehmood Ahmed (Superior university)


What is the purpose of Java sleep?

It pauses the execution of the program (or of the thread where it is invoked, if you have multiple threads), for the specified time.It pauses the execution of the program (or of the thread where it is invoked, if you have multiple threads), for the specified time.It pauses the execution of the program (or of the thread where it is invoked, if you have multiple threads), for the specified time.It pauses the execution of the program (or of the thread where it is invoked, if you have multiple threads), for the specified time.


If process stop its execution will thread also stop or it will continue to run?

Processes do not execute, it is the threads within a process that actually execute. All processes have at least one thread of execution, the main thread. If the main thread falls from scope, the process ends, taking all threads with it. This can lead to undefined behaviour if the threads are not terminated gracefully from within the main thread before it falls from scope.

Related Questions

What is interrpt request lines?

these are the block of code which tell the OS that a specific interrupt has arrived or to send interrupt request to some particular task. Interrupt is to stop the normal execution of the program and process the interrupt first according to it's priority in the interrupt vector table.


What is finally block in java?

The Finally block in java is used along with the try-catch statements. The usual structure is try { ..... } catch(Exception e){ .... } finally { ..... } In the finally block we usually have code that is used to perform clean up activities corresponding to the code in the try block. When an exception occurs in the try block control comes to the catch block and the rest of the code in the try block would not execute. In such cases we may need to have some code that cleans up the objects that we created/used in the try block. No matter what happens, the code inside the finally block would definitely execute. The best use of finally block is when we are connecting to the database. In the try block we would have the statements that instantiate the connection, prepared statement, result set etc. If the query fails then all these objects would be left open and the code would continue. So in the finally block we usually nullify these statements so that even in case of an error the unused objects are cleared. Example: Connection con; PreparedStatment PS; ResultSet rs; try{ con = ConnectionManager.getConnection(); PS = con.prepareStatement(query); rs = PS.executyQuery(); ...... } catch (Exception e){ e.printStackTrace(); } finally { con.close(); PS.close(); } In case of an error the connection & prepared statement objects would remain open if we do not have the finally block. Tip: Whatever code we have inside the finally block would be run definitely if the try block is invoked. We can even use the finally block to check if the try block was reached and executed. If the try executes then we can rest assured that the finally would also get executed.


In what episode of bleach did Ichigo stop Rukiya's execution?

it is bleach episode 54 named "day of execution"


What is the meaning of execution and terminating the programmes?

execution is the process by which a computer carries out the instructions of a computer program. terminating program is to stop the program from execution.


How can I programmatically stop the execution of a MATLAB program?

To programmatically stop the execution of a MATLAB program, you can use the "return" statement or the "error" function to exit the program at a specific point. This will halt the execution and return control to the calling function.


How do you stop harassing posts on my wall when none of the normal blocks are working?

Go to the harasser's page and block them. If that doesn't work, you should report them.


Can depakote make you stop feeling emotions?

Impossible. It can make it possible for a bipolar to finally feel "normal" emotions instead of uncontrollable extremes.


How do you stop execution of a program at a certain line?

Enable tracing


How many justices need to vote to stop execution?

In the US Supreme Court, a stay of execution requires five votes.


How can I programmatically stop a MATLAB script execution using a single command in MATLAB?

To programmatically stop a MATLAB script execution using a single command, you can use the "return" command. This command will immediately exit the current function or script, effectively stopping its execution.


How do you install glass block windows?

Glass block windows are typically installed in bathrooms. To put one in, cut out the area for block about 2.5 inches larger than the block itself. Next, nail in stop blocks along the window frame, and insert the window. Finally, inject expanding, weather proofing foam along the window's border.


What will stop a block of wood across the floor?

Friction will stop it.