Saturday, November 26, 2011

NullPointerException


packageorg.training.exceptions;

public class NullPointer {

      public static void main(String[] args) {
           
            new NullPointer();
      }
     
      public NullPointer(){
     
            Object[] object = {12,25,68,45};
           
            try {
                    for (Object o : object ) {
                          System.out.println(o);
                    }
                    // explicitly dereferencing object array
                    object = null;
                   
                    for (Object o : object ) {
                          System.out.println(o);
                    }
                   
            }catch(NullPointerException nfe) {
                  nfe.printStackTrace();
            }
    }

}

Output:

12
25
68
45
java.lang.NullPointerException
      at org.training.exceptions.NullPointer.<init>(NullPointer.java:21)
      at org.training.exceptions.NullPointer.main(NullPointer.java:7)

ClassNotFoundException


ClassNotFoundException is one of the most commonly occurring exceptions. This exception is thrown when the executing program is unable to find the defined class. This could be because of various reasons, wrong classpath, older version of jar file etc...

packageorg.training.exceptions;

public class ClassNotFound {

      public static void main(String[] args) {
           
            new ClassNotFound();
      }
     
      publicClassNotFound(){
     
            try {
                  // load the java.lang.Strung class,
                  // this would thrown an ClassNotFoundException
 because there is no class called “Strung” defined in the java.lang package  
             Class c = Class.forName("java.lang.Strung");
            }catch(ClassNotFoundException cnfe) {
                  cnfe.printStackTrace();
            }
    }

}

Output:

java.lang.ClassNotFoundException: java.lang.Strung
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Unknown Source)
      at org.training.exceptions.ClassNotFound.<init>(ClassNotFound.java:14)
      at org.training.exceptions.ClassNotFound.main(ClassNotFound.java:7)

NoSuchMethodException


packageorg.training.exceptions;

importjava.lang.reflect.Method;

public class NoSuchMethod {

      public static void main(String[] args) {
           
            new NoSuchMethod();
      }
     
      public NoSuchMethod(){
     
            try {
                  // load the java.lang.String class, if not found throws            ClassNotFoundException
                  Class c = Class.forName("java.lang.String");
           
                  try {
                  Class[] arguments = new Class[2];

                  // search for method called "someMethod" with two parameters
                  // since there is no method called "someMethod(arg1, arg2)", in the java.lang.String class, this throws an NoSuchMethodException
                  Method m = c.getDeclaredMethod("someMethod", arguments);

                }catch(NoSuchMethodException nsme) {
                      nsme.printStackTrace();
                }
          }catch(ClassNotFoundException cnfe) {
                  cnfe.printStackTrace();
            }
    }

}

Output:

java.lang.NoSuchMethodException: java.lang.String.someMethod(null, null)
      at java.lang.Class.getDeclaredMethod(Unknown Source)
      at org.training.exceptions.NoSuchMethod.<init>(NoSuchMethod.java:19)
      at org.training.exceptions.NoSuchMethod.main(NoSuchMethod.java:9)

Friday, November 25, 2011

NumberFormatException


packageorg.training.exceptions;

public class NumberFormat {

      public static void main(String[] args) {
           
            String string = "hello";
           
            try {
                  // this parsing doesn't work as hello is not a number
                  Integer integer = Integer.parseInt(string);
                  System.out.println(integer++);
            }catch(NumberFormatException nfe) {
                  System.out.println("Number Format Exception caught, check what you are parsing");
                  nfe.printStackTrace();
            }
}


Output:

Number Format Exception caught, check what you are parsing
java.lang.NumberFormatException: For input string: "hello"
      at java.lang.NumberFormatException.forInputString(Unknown Source)
      at java.lang.Integer.parseInt(Unknown Source)
      at java.lang.Integer.parseInt(Unknown Source)
      at org.training.exceptions.NumberFormat.main(NumberFormat.java:11)

NegativeArraySizeException


packageorg.training.exceptions;

public classNegativeArraySize {

      public static void main(String[] args) {
           
            int array[] = {1,2,3,4,5};
           
            try {
                  array = new int[-8];
            }catch(NegativeArraySizeException nase) {
                  System.err.println("Negative Array Size Exception has occured, check array initialization again. ");
            }
           
            for (int i: array) {
                  System.out.println(i);
            }
      }

}


Output:

Negative Array Size Exception has occurred, check array initialization again.

1
2
3
4
5

IndexOutOfBoundsException



packageorg.training.exceptions;

public classIndexOutOfBounds {

      public static void main(String[] args) {
           
            String s = "abc";
           
            for (int i = 0 ; i < 4 ; i++) {
                  try {
                        System.out.println(s.charAt(i));
                  }catch(IndexOutOfBoundsException iobe){
                        System.err.println("Index Out Of Bounds Exception has occured");
                  }
            }

      }

}


Output:
a
b
c
Index Out Of Bounds Exception has occured