Friday, November 25, 2011

IllegalArgumentException


package org.training.exceptions;

public class IllegalArgument {

      public static void main(String[] args) {
           
            IllegalArgument ia = new IllegalArgument();
            try {
                  ia.simpleMethod("Hello");
                  ia.simpleMethod(null);
            }catch(IllegalArgumentException iae) {
                  System.err.println("Illegal Argument Exception has been caught, check your arguments again");
                  iae.printStackTrace();
            }
      }
     
      public void simpleMethod(String argument) {
            if(argument == null) {
                  throw new IllegalArgumentException();
            }else
                  System.out.println(argument);
      }

}


Output:
Hello
Illegal Argument Exception has been caught, check your arguments again
java.lang.IllegalArgumentException
      at org.training.exceptions.IllegalArgument.simpleMethod(IllegalArgument.java:19)
      at org.training.exceptions.IllegalArgument.main(IllegalArgument.java:10)

ClassCastException


A class cast exception is thrown by Java when you try to cast an Object of one data type to another.

Java allows us to cast variables of one type to another as long as the casting happens between compatible data types.

For example you can cast a String as an Object and similarly an Object that contains String values can be cast to a String.


package com.training.exceptions;

class Vehicle {
     
      public void doStuff() {
            System.out.println("Vehicle");
      }
}

class Car extends Vehicle{
     
      public void doStuff() {
            System.out.println("Car");
      }
}

class Bike extends Vehicle {
     
      public void doStuff() {
            System.out.println("bike");
      }
}

public class ClassCast {

      public static void main(String[] args) {
     
            try {
                    // this will throw a ClassCastException
                    Vehicle v = (Bike)new Vehicle();
                    v.doStuff();
            }catch(ClassCastException cce) {
                  System.err.println("Class Cast Exception has been caught, check your code again");
                  cce.printStackTrace();
            }          
      }

}

Output:
Class Cast Exception has been caught, check your code again
java.lang.ClassCastException: com.training.inheritance.Vehicle
      at com.training.inheritance.VehicleTest.main(VehicleTest.java:30)

ArrayIndexOutOfBoundsException


package org.training.exceptions;

public class ArrayIndexOutOfBounds {

      public static void main(String[] args) {
           
            int array[] = new int[3];
           
            for (int i = 0; i < 4; /* this condition checks for more space that allocated for the array */ i++) {
                 
                  try {
                        array[i] = i;
                  }catch(ArrayIndexOutOfBoundsException aiobe) {
                        System.err.println("Array Index Out Of Bounds Exception has been caught, check your code again");
                        aiobe.printStackTrace();
                  }
            }
           
            for ( int x : array) {
                  System.out.println(x);
            }

      }

}

Output:
0
1
2
Array Index Out Of Bounds Exception has been caught, check your code again
java.lang.ArrayIndexOutOfBoundsException: 3
      at org.training.exceptions.ArrayIndexOutOfBounds.main(ArrayIndexOutOfBounds.java:12)

ArithmeticException


ArithmeticException is to catch arithmetic errors, like divide by zero. Following is the code that demonstrates this.


package org.training.exceptions;

public class Arithmetic {

      public static void main(String[] args) {
           
            int x = 0;
            int y = 10;
            int z = 0;
           
            try {
                  // this is prone for arithmetic exception,
                  z = y / x;
            }catch(ArithmeticException ae) {
                  System.err.println("Arithmetic Exception has been caught, check your code again.");
                  ae.printStackTrace();
            }

      }

}

Output:
Arithmetic Exception has been caught, check your code again.
java.lang.ArithmeticException: / by zero
      at org.training.exceptions.Arithmetic.main(Arithmetic.java:13)