bimal chawla
bimal chawla

Reputation: 449

finally method with try in java

class DaemonThread extends Thread {

public void run() {
    System.out.println("Entering run method");

    try {
        System.out.println("In run Method: currentThread() is"
            + Thread.currentThread());

        while (true) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException x) {
                System.out.println("hi");
            }

            // System.out.println("In run method: woke up again");

            finally {
                System.out.println("Leaving run1 Method");
            }
        }
    } finally {
        System.out.println("Leaving run Method");
    }

}

public static void main(String[] args) {
    System.out.println("Entering main Method");

    DaemonThread t = new DaemonThread();
    t.setDaemon(true);
    t.start();

    try {
        Thread.sleep(900);
    } catch (InterruptedException x) {}

    System.out.println("Leaving main method");
}

}

why second finally method not run...as i know finally method must have have to run whatever the condition is ..but in this case only first finally method, why not second finally run.

Upvotes: 1

Views: 1270

Answers (5)

Parvin Gasimzade
Parvin Gasimzade

Reputation: 26012

In theory it should run the second finally method, but since it is out of the while(true) loop which never ends then it cannot be accessed.

Upvotes: 2

Op De Cirkel
Op De Cirkel

Reputation: 29473

I guess you expect that on JVM exit because the thread is Daemon, would gracefully exit the loop automatically. That is not true. Daemon threads simply die (at the position in the code currently executing)

Upvotes: 0

UVM
UVM

Reputation: 9914

It will never execute the finally block as while loop is always TRUE.Also, from java comments

"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."

Upvotes: 0

Kazekage Gaara
Kazekage Gaara

Reputation: 15052

Your code shows that your while loop won't end. Hence there is no question of reaching the outer finally block.

Just use any other condition, and you might get what you want to achieve. For example:

public void run() {
    System.out.println("Entering run method");
    int flag = 1;
    try {
        System.out.println("In run Method: currentThread() is"
            + Thread.currentThread());

        while (flag == 1) {
            try {
                Thread.sleep(500);
                 flag = 0;
            } catch (InterruptedException x) {
                System.out.println("hi");
            }

            // System.out.println("In run method: woke up again");

            finally {
                System.out.println("Leaving run1 Method");
            }
        }
    } finally {
        System.out.println("Leaving run Method");
    }

}

Upvotes: 0

Jean Logeart
Jean Logeart

Reputation: 53829

The println statement is never reached because of the while(true) loop that never ends!

If you ever leaves that loop, then the second finally block would be executed.

Upvotes: 6

Related Questions