Thursday, December 11, 2008

Simple Java Program to understand Exception Handling

/*------------------------------------------------------------------------
Simple Java Program to understand Exception Handling
------------------------------------------------------------------------*/
class exceptionexample{
public static void main(String args[]){
int i;
try{
for(i=0;i<3;i++){
System.out.println(args[i]);
}
} catch (ArrayIndexOutOfBoundsException e){
System.out.println("A minimum of 3 arguments arerequired");
} }}
/*------------------------------------------------------------------------
Output without the "try" block
------------------------------------------------------------------------
C:\j2sdk1.4.2_04\bin>javac exceptionexample.javaC:\j2sdk1.4.2_04\bin>java exceptionexample 1 2 3 4123C:\j2sdk1.4.2_04\bin>java exceptionexample 1 212Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at exceptionexample.main(exceptionexample.java:5)
------------------------------------------------------------------------
Output with the "try" block
------------------------------------------------------------------------
C:\j2sdk1.4.2_04\bin>java exceptionexample 1 212A minimum of 2 arguments are required
------------------------------------------------------------------------*/

No comments: