4:50 am - Wednesday September 26, 2018

Java program using Array Exception

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

class Exception {
public static void main(String args[]) {
// The exception should be define under the Try-Catch block
try {
// Initialize an array size with the string variable
String s1 = args[0];
String s2 = args[1];
// Casting is done here
int i = Integer.parseInt(s1);
int j = Integer.parseInt(s2);
// The variables I and j have 0 and 1 here i/j is infinity so it
// calls the arrayindexoutofbound exception
int k = i / j;
System.out.println(k);
} catch (ArithmeticException a) {
System.out.println(a.getMessage());
}
// The exception is called
catch (ArrayIndexOutOfBoundsException a) {
System.out.println("give the proper input");
}
}
}

Output:

D:\Candid Java\Util\Exception\mm>javac Exception.java

D:\CandidJava\Util\Exception\mm>Exception

give the proper input.

Filed in: Z-A All Java Codes

No comments yet.

Leave a Reply