Friday, November 25, 2011

IndexOutOfBoundsException



package org.training.exceptions;

public class IndexOutOfBounds {

      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

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)