Java Exception catch null pointer exception

Description

Java Exception catch null pointer exception

public class Main {
   public static void main(String[] args) {

      System.out.println(test("1234"));
      System.out.println(test("12345"));
      System.out.println(test("123456"));
      System.out.println(test(null));

   }//from w  w  w.  j a v a2 s. c  o  m

   private static boolean test(String aString) {
      try {
         return aString.length() > 5;
      } catch (NullPointerException e) {
         System.out.println("An Exception Happened!");
         return false;
      }
   }
}



PreviousNext

Related