Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Saturday 2 February 2019

SECOND WEEK TERM WORK Program 1

1) Write a Java Program that generates:
a. ArithmeticException when no arguments are passed
b. ArrayIndexOutOfBoundsException when one argument is passed
c. NullPointerException when two arguments are passed
d. NegativeArraySizeException when three arguments are passed
Display the stack trace of the exceptions generated.
When more than three arguments are passed, then the program should print all the command-
line arguments passed to it.

package exception1;

public class exceptiondemo {
public static void main(String[] args) {
int a;
int array[] = new int[3];
String s = null;
try {
if (args.length == 0) {
System.out.println("Zero Arguments");
a = 10 / 0;
}
if (args.length == 1) {
System.out.println("One Arguments");
array[6] = 10;
}
if (args.length == 2) {
System.out.println("Two Arguments");
s.length();
}
if (args.length == 3) {
System.out.println("Three Arguments");
array[-1] = 10;
} else {
System.out.println("More than 3 arguments");
for (int i = 0; i < args.length; i++) {
System.out.println(" " + args[i]);
}
}
} catch (NegativeArraySizeException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();

}

}

}

0 comments:

Post a Comment