UEC2023123 Assignment11
UEC2023123 Assignment11
class SuperSubCatch {
public static void main (String args []) {
try {
int a = 0;
int b = 42/a;
}
catch (Exception e) {
System.out.println ("Generic Exception catch.");
}
catch (ArithmeticException e) {
System.out.println ("This is never reached.");
}
}
}
SuperSubCatch.java:10: error: exception ArithmeticException has already been caught
catch (ArithmeticException e) {
^
class MultiCatch {
public static void main (String args []) {
try {
int a = args.length;
System.out.println ("a" = +a);
int b = 42/a;
int c[] = {1};
c[42] = 99;
}
catch (ArithmeticException e) {
System.out.println ("Divide by 0: " +e);
}
catch (ArrayIndexOfBoundsException e) {
System.out.println ("Array Index oob: " +e);
}
System.out.println ("After catch try block");
}
}
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ javac MultiCatch.java
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ java MultiCatch
a=0
Divide by 0: java.lang.ArithmeticException: / by zero
After catch try block
class NesTry {
public static void main (String args []) {
try {
int a = args.length;
int b = 42/a;
System.out.println ("a " + a);
try {
if (a == 1) a = a/a-a;
if (a == 2) {
int c[] = {1};
c[42] = 91;
}
}
catch (ArithmeticException e) {
System.out.println (e);
}
}
}
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ javac NesTry.java
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ java NesTry
java.lang.ArithmeticException: / by zero
class FinallyDemo {
static void procA () {
try {
System.out.println ("Inside procA.");
throw new RuntimeException ("demo");
}
finally {
System.out.println ("prosA's finally.");
}
}
public static void main (String args []){
try {
procA ();
}
catch (Exception e){
System.out.println ("Exception Caught.");
}
procB ();
procC ();
}
}
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ javac FinallyDemo.java
(base) ccoew@etccomplab5:~/Desktop/UEC2023123$ java FinallyDemo
Inside procA.
prosA's finally.
Exception Caught.
Inside procB.
prosB's finally.
Inside procA.
prosC's finally.