top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

In java is the finally block is always guaranteed to be execute?

+3 votes
46 views
posted Sep 5, 2013 by Arvind Singh

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

+1 vote

Finally will be called even if an exception was thrown in Try block.

The only time finally won't be called is if you call System.exit() or if the JVM crashes first.

answer Sep 5, 2013 by Satyabrata Mahapatra
Similar Questions
0 votes

Like I have a tiny java game in which name of movie is hidden and we have to guess that. Here if your guess is correct that letter will appear at correct place and if wrong you will loose one chance. When we run the code, it runs sometimes without taking any input which it should not do. The java code is following.

import java.util.*;
import java.io.*;
import java.lang.*;
class StrDemo
{
    static StringBuilder change(StringBuilder sb)
    {
        StringBuilder sb1=new StringBuilder("");
        int len=sb.length();
        for(int i=0;i<len;i++)
        {
            if(sb.charAt(i)!=' ')
                sb1.append("*");
            else
                sb1.append(" ");
        }
        return sb1;
    }

    public static void main(String a[])
        throws Exception
        {
            StringBuilder sb2=new StringBuilder("hum sath sath hain");
            StringBuilder sb3=change(sb2);
            int j=0;
            StringBuilder sb4=new StringBuilder("Bollywood");

            while(true)
            {
                System.out.println(sb4);
                System.out.println(sb3);
                System.out.println("Enter Char");

                Scanner sc=new Scanner(System.in);

                char ch=(char)System.in.read();
                int c=0;
                int len=sb2.length();

                for(int i=0;i<len;i++)
                {
                    if(sb2.charAt(i)==ch)
                    {
                        sb3.setCharAt(i,ch);
                        c++;
                    }
                }

                if(c==0)
                    sb4.setCharAt(j++,'*');

                if(sb4.equals("*********"))
                    System.out.println("you lost...");
                else if(sb3.equals("hum sath sath hain"))
                    System.out.println("...u won");
            }   
        }
}

Useful Links with Similar Problem
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#470/147, 3rd Floor, 5th Main,
HSR Layout Sector 7,
Bangalore - 560102,
Karnataka INDIA.
QUERY HOME
...