Explain this code, please. public class Exception1 extends Exception { public Ex
ID: 3583064 • Letter: E
Question
Explain this code, please.
public class Exception1 extends Exception {
public Exception1() {
}
public Exception1(String msg) {
super(msg);
}
}
public class Exception2 extends RuntimeException {
public Exception2() {
}
public Exception2(String msg) {
super(msg);
}
}
public class ExceptionTest {
public static String spindle(int dat) throws Exception1 {
try {
if (dat == 1) {
return "one";
} else if (dat == 2) {
throw new IllegalArgumentException("bad arg");
} else if (dat == 3) {
throw new Exception1("bad");
} else if (dat == 4) {
throw new Exception2("worse");
} else {
throw new NullPointerException();
}
} catch (NullPointerException npe) {
return "null pointer";
} finally {
System.out.println("finally done");
}
}
public static void mutilate(int dat) {
try {
System.out.println(spindle(dat));
} catch (NullPointerException npe) {
System.out.println("caught NPE");
} catch (IllegalStateException ise) {
System.out.println("in an illegal state");
} catch (RuntimeException re) {
System.out.println("caught runtime exception");
} catch (Exception e) {
System.out.println("caught exception");
}
}
public static void main(String[] args) {
mutilate(1);
mutilate(2);
mutilate(3);
mutilate(4);
mutilate(5);
}}
Output
finally done
one
finally done
caught runtime exception
finally done
caught exception
finally done
caught runtime exception
finally done
null pointer
public class Exception1 extends Exception {
public Exception1() {
}
public Exception1(String msg) {
super(msg);
}
}
public class Exception2 extends RuntimeException {
public Exception2() {
}
public Exception2(String msg) {
super(msg);
}
}
public class ExceptionTest {
public static String spindle(int dat) throws Exception1 {
try {
if (dat == 1) {
return "one";
} else if (dat == 2) {
throw new IllegalArgumentException("bad arg");
} else if (dat == 3) {
throw new Exception1("bad");
} else if (dat == 4) {
throw new Exception2("worse");
} else {
throw new NullPointerException();
}
} catch (NullPointerException npe) {
return "null pointer";
} finally {
System.out.println("finally done");
}
}
public static void mutilate(int dat) {
try {
System.out.println(spindle(dat));
} catch (NullPointerException npe) {
System.out.println("caught NPE");
} catch (IllegalStateException ise) {
System.out.println("in an illegal state");
} catch (RuntimeException re) {
System.out.println("caught runtime exception");
} catch (Exception e) {
System.out.println("caught exception");
}
}
public static void main(String[] args) {
mutilate(1);
mutilate(2);
mutilate(3);
mutilate(4);
mutilate(5);
}}
Explanation / Answer
Exception1,Exception2 class:
the above two classes are extended from Exception class which makes them custom exception classes.Hence, these can be used to handle exceptions.
ExceptionTest class:
This is your driver class which throws exceptions of the above two classes.
In spindle(), you accept an int as argument. And based on the value [passsed, youa re throwing the respective exceptions.
In mutilate(), you accept int as an argument. You display the error/messgae by calling spindle() directly and if null or something other than int is passed, you are throwing the respective exceptions.
In main(),
mutilate(1)
this will call spinlde(1) which returns one and executes finally.
Hence, "finally done" gets printed and then one gets printed.
mutilate(2);
this will call spinlde(2). This throws IllegalArgumentException to calling function and finally gets executed.
So "finally done" gets printed.
As IllegalArgumentException is not handled in the same function, the control goes back to calling function
Now in mutilate(), as IllegalArgumentException is not caught, this will be caught by RuntimeException and hence "caught runtime exception" gets printed.
mutilate(3);
this will call spinlde(3). This throws Exception1 to calling function and finally gets executed.
So "finally done" gets printed.
As Exception1 is not handled in the same function, the control goes back to calling function
Now in mutilate(), as Exception1 is not caught, this will be caught by Exception and hence "caught exception" gets printed.
mutilate(4);
this will call spinlde(4). This throws Exception2 to calling function and finally gets executed.
So "finally done" gets printed.
As Exception2 is not handled in the same function, the control goes back to calling function
Now in mutilate(), as Exception2 is not caught, this will be caught by RuntimeException (because Exception2 is extended from RunTimeException) and hence "caught runtime exception" gets printed.
mutilate(5);
this will call spinlde(5). This throws NullPointerException and finally gets executed.
So "finally done" gets printed.
As NullPointerException is handled in the same function,"caught NPE" gets printed. ANd the control goes back to calling function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.