Try with Finally Java (without Catch)


Generally, programmer writes try in combination with catch. Using finally block is optional. But it is possible to have try with finally without catch block) and compiler accepts clearly. Observe the following code.
public class Demo
{
   public static void main(String args[])
   {
       try
       {
           System.out.println(10/0);
       }
       finally
       {
           System.out.println();
           System.out.println("From finally block");
       }
       System.out.println("Hello 1");
   }
}

Try with Finally

In the above code, catch block is missing. But Hello1 is not printed as the ArithmeticException thrown by the system is not handled. This type of programming is of only theoretical importance but no much real time importance.

Would you like read some more confusing concepts?

1. Why Java introduced StringBuilder with the same functionality (usage) of StringBuffer?

2. Why split() method was introduced when Java has already StringTokenizer?
Ans: StringTokenizer comes with JDK1.0 version where as split() method comes with JDK1.4 version. StringTokenizer beongs to java.util package and split() is defined in String class of java.lang package. One major difference is, split() supports regular expressions but StringTokenizer does not. If tokens (independent words) are required in an array form for easy retrieval and operation, split() is preferred. No operations on elements and only retrieval, StringTokenizer is preferred. It is jsut only coding requirements.

3. Can you code like this.

System.out.println(pow(2,4)); // Math class is not written

instead of traditional

System.out.println(Math.pow(2,4)); // Math is written
Ans: Yes, it is possible from JDK1.5 version with a new feature known as static imports.

4. Can you use C-lang printf() in Java with all %d and %s etc options.?
Ans: Yes, it is possible from JDK1.5 version.

5. Can you use C-lang enums in Java?
Ans: Yes, it is possible from JDK1.5 version.

2 thoughts on “Try with Finally Java (without Catch)”

Leave a Comment

Your email address will not be published.