Java Exception Handling (try, catch, finally) Quiz
40 in-depth questions (20 with code) covering types of exceptions, try/catch syntax, finally block purpose, stack traces, and best practices in Java exception handling.
Question 1
What are the three main types of exceptions in Java?
Question 2
What is a checked exception?
Question 3
What is an unchecked exception?
Question 4
What is the purpose of the finally block?
Question 5
What is a stack trace?
Question 6
When should you catch an exception?
Question 7
What is the output of this basic try-catch example?
public class Test {
public static void main(String[] args) {
try {
int x = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Caught: " + e.getMessage());
}
}
}Question 8
What happens if an exception is thrown in a finally block?
Question 9
What is the difference between throw and throws?
Question 10
What is the result of this finally execution example?
public class Test {
public static void main(String[] args) {
System.out.println(test());
}
static int test() {
try {
return 1;
} finally {
return 2;
}
}
}Question 11
What is try-with-resources?
Question 12
When should you create custom exceptions?
Question 13
What is the output of this nested try-catch example?
public class Test {
public static void main(String[] args) {
try {
try {
throw new RuntimeException("Inner");
} catch (RuntimeException e) {
System.out.println("Inner catch: " + e.getMessage());
throw e;
}
} catch (RuntimeException e) {
System.out.println("Outer catch: " + e.getMessage());
}
}
}Question 14
What is exception chaining?
Question 15
What is the purpose of multi-catch blocks?
Question 16
What is the result of this try-with-resources example?
class Resource implements AutoCloseable {
public void close() { System.out.println("Closed"); }
}
public class Test {
public static void main(String[] args) {
try (Resource r = new Resource()) {
System.out.println("Using resource");
throw new RuntimeException("Error");
} catch (Exception e) {
System.out.println("Caught: " + e.getMessage());
}
}
}Question 17
When should you use runtime exceptions vs checked exceptions?
Question 18
What is the output of this exception propagation example?
public class Test {
static void method1() { throw new RuntimeException("From method1"); }
static void method2() { method1(); }
static void method3() { method2(); }
public static void main(String[] args) {
try {
method3();
} catch (RuntimeException e) {
System.out.println("Caught in main: " + e.getMessage());
}
}
}Question 19
What is the best practice for exception messages?
Question 20
What happens with this suppressed exception example?
class Resource implements AutoCloseable {
public void close() throws Exception {
throw new Exception("Close failed");
}
}
public class Test {
public static void main(String[] args) {
try (Resource r = new Resource()) {
throw new RuntimeException("Main error");
} catch (Exception e) {
System.out.println("Caught: " + e.getMessage());
for (Throwable t : e.getSuppressed()) {
System.out.println("Suppressed: " + t.getMessage());
}
}
}
}Question 21
Why should you avoid catching Exception or Throwable?
Question 22
What is the output of this finally return example?
public class Test {
static String test() {
try {
return "try";
} catch (Exception e) {
return "catch";
} finally {
System.out.println("finally executed");
}
}
public static void main(String[] args) {
System.out.println(test());
}
}Question 23
What is the difference between Error and Exception?
Question 24
What is the result of this multi-catch example?
public class Test {
public static void main(String[] args) {
try {
if (args.length > 0) {
throw new IllegalArgumentException("Bad arg");
} else {
throw new NullPointerException("Null");
}
} catch (IllegalArgumentException | NullPointerException e) {
System.out.println("Caught: " + e.getClass().getSimpleName());
}
}
}Question 25
When should you declare exceptions in method signatures?
Question 26
What is the output of this exception re-throwing example?
public class Test {
public static void main(String[] args) {
try {
throw new RuntimeException("Original");
} catch (RuntimeException e) {
throw new IllegalStateException("Wrapped", e);
}
}
}Question 27
Why is it important to close resources properly?
Question 28
What is the result of this nested exception handling?
public class Test {
public static void main(String[] args) {
try {
try {
throw new Exception("Inner");
} finally {
System.out.println("Inner finally");
}
} catch (Exception e) {
System.out.println("Caught: " + e.getMessage());
} finally {
System.out.println("Outer finally");
}
}
}Question 29
What is the best practice for logging exceptions?
Question 30
What happens in this try-with-resources with exception in close?
class Resource implements AutoCloseable {
public void close() throws Exception {
throw new Exception("Close error");
}
}
public class Test {
public static void main(String[] args) throws Exception {
try (Resource r = new Resource()) {
System.out.println("Using resource");
}
}
}Question 31
When should you use checked exceptions?
Question 32
What is the output of this exception in constructor example?
class TestClass {
TestClass() throws Exception {
throw new Exception("Constructor failed");
}
}
public class Test {
public static void main(String[] args) {
try {
new TestClass();
} catch (Exception e) {
System.out.println("Caught: " + e.getMessage());
}
}
}Question 33
Why should you avoid empty catch blocks?
Question 34
What is the result of this exception precedence example?
public class Test {
public static void main(String[] args) {
try {
throw new RuntimeException("Runtime");
} catch (Exception e) {
System.out.println("Caught Exception: " + e.getMessage());
} catch (RuntimeException e) {
System.out.println("Caught Runtime: " + e.getMessage());
}
}
}Question 35
What is the best practice for exception handling in layered applications?
Question 36
What is the output of this finally with return example?
public class Test {
static boolean test() {
try {
return true;
} finally {
return false;
}
}
public static void main(String[] args) {
System.out.println(test());
}
}Question 37
When should you use try-catch in constructors?
Question 38
What is the result of this suppressed exception access?
public class Test {
public static void main(String[] args) {
try (java.io.StringReader r = new java.io.StringReader("test")) {
throw new RuntimeException("Main");
} catch (Exception e) {
System.out.println("Main: " + e.getMessage());
Throwable[] suppressed = e.getSuppressed();
if (suppressed.length > 0) {
System.out.println("Suppressed: " + suppressed[0].getMessage());
}
}
}
}Question 39
Why is proper exception handling important for security?
Question 40
Mastering exception handling means you now write code that:
