Given the following program, which statements are true?
public class ExceptionLabExercise {
public static void main(String[] args) {
try {
System.out.println(args[0]);
} finally {
System.out.println("FINALLY");
}
}
}
C. Given the following classes,
public class TestClass {
public static void main(String[] args) throws A {
try {
f();
} finally {
System.out.println("Done.");
|
} catch (Exception1 e) {
throw e;
}
}
}
public class Exception1 extends Throwable{}
Test Stem / Question |
Choices |
6: What is the problem with the above code? |
A: The finally statement must be preceeded by the catch clause |
B: Exceptions cannot be extended |
|
C: It is illegal to throw an exception |
|
D: Nothing is wrong |
|
7: What should be done with the code? |
A: Move the catch clause before the finally clause. |
B: Do not use Throwable as the supertype of Exception 1. |
|
C: Do not throw the exception. |
|
D: Nothing is wrong |
|
8: What happens after you have been able to fix the code? |
A: “Done” is printed and an exception is shown in the stack |
B: “Done” is printed |
|
C: Exception is shown in the stack |
|
D: Nothing is wrong |
public class JavaApplication1 {
public static void main(String[] args) {
RuntimeException exception = null;
throw exception;
}
}
Test Stem / Question |
Choices |
9: What is the problem with the above code? |
A: null exception cannot be thrown |
B: the main method must be declared with throws statement |
|
C: It is illegal to throw an exception |
|
D: Nothing is wrong |
|
10: How should you fix the code? Choose from the following:
|
A: A & B |
B: A only |
|
C: B only |
|
D: All is correct |
Fix the codes and show the correct answer
Answer 4:C: It printed finally
as it throws exception and executes in finally block
Answer 5: A: Check the length of the array before printing the arguments
Answer 6:
A: The finally statement must be preceeded by the catch clause
Answer 7:
A: Move the catch clause before the finally clause.
Answer 8:
Done” is printed and an exception is shown in the stack
prints the stack trace because it is re throwing the exception
Answer 9:
A: null exception cannot be thrown
Answer 10:
Instantiate the exception
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me
Get Answers For Free
Most questions answered within 1 hours.