Tuesday, 13 October 2015

Exception handling example in java


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
try{
int k = args.length;
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
Sec obj = new Sec();
obj.sum(i, j);
}

catch(ArrayIndexOutOfBoundsException a){

a.printStackTrace();
System.out.println("Please pass runtime arguments while executing the program");
}
finally{
System.out.println("Finally block");
}
}
}


public class Sec {
void sum(int i,int j){
int k = i+j;
System.out.println("The value is " +k);
}
}

output:-
java.lang.ArrayIndexOutOfBoundsException: 0
at First.main(First.java:8)
Please pass runtime arguments while executing the program
Finally block

No comments:

Post a Comment