When does the finally clause in java exception block never executes?

The finally clause in the try-catch exeception block always executes, irrespective of the occurence of exeception. This is applicable for the normal java program flow. If the execution flow is stopped irreversibly before the finally clause, then the finally block will not be executed.

How can the user achieve that in Java?
Include “System.exit(1);” before the finally block and stop the execution flow of the java program.

This Java Exception tutorial was added on 04/05/2008.

Comments on "When does the finally clause in java exception block never executes?" Tutorial:

  1. Amitabha says:

    Is there any example situation to avoid finally block and stop the JVM by System.exit(n)?

  2. ismail says:

    can i use finalize or finalization

  3. Irfan says:

    well said
    i didnt know it before

  4. Vishal ArbunePatil says:

    i use all the clause e.g try ,catch, throw,throws & finally…… Finally is always be executed i agree with that… but what i mention in try block that’s why finally blk not executed…..

    Plz… guide me….

  5. anitha says:

    finalize or finalisation cannot be used ismail….their functionality is different

  6. Nagarjuna Yerrapothu says:

    could you please give the difference between Final Keyword and Private access modifier?

  7. Prashant Zombade says:

    @Nagarjuna
    private members can be accessed within the class only. You will get compilation otherwise.
    But In case of Final
    1. Final Variables can not be modified after they have been initialized.
    2. Final class can not be extended.
    3. Final methods can not be overridden.
    Final variables can be marked private and vice-a-versa.
    Hope this solves your doubts.

  8. Anonymous says:

    which will effect if i keep init() method explicitly in jsp? and how it will execute?

  9. sangeetha says:

    Thanks for the reply i was asked this question in a interview

  10. Anonymous says:

    Sir I want to tell that finally block will not execute when the finally block itself have an exception ,Or the reason u mentioned

  11. Soumya says:

    Can a finally be placed before a catch statement??

  12. shivam dubey says:

    finalize or finalisation cannot be used ismail……finalize use in garbage collection.

  13. shankardayal says:

    can we give any literal to the exit(args) method

  14. Anonymous says:

    Hey Joe U didn’t mention where to add this System.out(1). means in try block or catch block???

  15. Laksh says:

    there is also a concept of Daemon thread to stop finally block or to prevent finally from execution. Do any1 know dis?

  16. Ashok Kumar says:

    Final is keyword that can be used with the variable, method and class.
    Following are the usage of Final Keyword.

    EX 1:

    final int age=30;
    Cannot change the value of the age. Its like a constant.

    EX : 2
    public final void display(){

    //Your code goes here.
    }
    Cannot override the method in the sub-class.

    EX : 3

    final class Test {

    // Member Variable and methods

    }

    Cannot extend this class

    2. Private is a access specifier and it applies to variable, method and classes.

    Variable – we can use only with in the class but we can change the value of the variable.

    Method – We can use only with in the class, cannot access outside the class and so on.

    Hope this answers your question

    Thanks
    Ashok

  17. Jeff says:

    to clarify, Final Variables cannot be reassigned, they can be modified.

    final Map m = new HashMap();
    m.add(“one”, 1); // this is modification

    Map n = new HashMap();
    m = n; // this is reassignment and not allowed for final variables.

    final String s = “one”; // initialization
    String t = “two”
    s = t; // reassignment, not allowed
    t = “three” // non-final reassignment, allowed

    final int i = 0;
    i = 1; // reassignment, not allowed.

    Another way to say it is that final variables may not be modified by reassignment, however, the internal object state may be changed (modified).

    Hope that helps.

  18. Jeff says:

    oops, should have said m.put(“one”, 1) rather than m.add… sorry for that confusion

  19. Shrikant Kale says:

    Finally block is always executed except some of the following cases.
    If the JVM exits while the try or catch code is being executed, then the finally block may not execute.
    e.g. System.exit(0);
    If the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.
    e.g. Thread.interrupted();

  20. sanjay says:

    what happen when finally block throw an exception ?

  21. Prabh says:

    Nice post..!

  22. Samba says:

    when the finally clause in not executed?
    A).system.exit(0);

    This is one way.is there any other way not to execute finally block.

Comments are closed for this "When does the finally clause in java exception block never executes?" tutorial.