Friday, September 22, 2017

New Feature of Java Standard Edition (JSE 7)

a) Multiple Catch blocks
b) String in switch statement
c) try with resources
d) Introducing binary and underscore within literal


package com.exception.java7;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class CatchException {

     public void multipleCatchBlocks() {
          try {
              FileReader file = new FileReader("abc.txt");
          } catch (NumberFormatException |
             NullPointerException | FileNotFoundException f) {
              System.out.println(f);
          }
     }

     public void testTryWithResourcesStatement()
     throws   FileNotFoundException, IOException {
          try (FileInputStream in = new
           FileInputStream("java7.txt"))   
     {
              System.out.println(in.read());
          }
     }

     public void testBinaryIntegralLiterals() {
          int binary = 0b1001_101;
          if (binary == 8) {
              System.out.println(true);
          } else {
              System.out.println(false);
          }
     }

     public void testUnderscoresNumericLiterals() {
          int oneMillion_ = 1_000_000;
          int oneMillion = 1000000;
          if (oneMillion_ == oneMillion) {
              System.out.println(true);
          } else {
              System.out.println(false);
          }
     }
}

No comments:

Post a Comment