Challenge - 5 Problems
Custom Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediateOutput of custom exception throwing
What will be the output when this Java code runs?
Java
class MyException extends Exception { public MyException(String message) { super(message); } } public class Test { public static void check(int num) throws MyException { if (num < 0) { throw new MyException("Negative number: " + num); } else { System.out.println("Number is " + num); } } public static void main(String[] args) { try { check(-5); } catch (MyException e) { System.out.println("Caught: " + e.getMessage()); } } }
Attempts:
2 left
π‘ Hint
Look at how the exception is thrown and caught in the try-catch block.
β Incorrect
The method throws a custom checked exception when the number is negative. The main method catches it and prints the message.
π§ Conceptual
intermediateUnderstanding checked vs unchecked exceptions
Which statement correctly describes the difference between checked and unchecked exceptions in Java?
Attempts:
2 left
π‘ Hint
Think about compiler requirements for exception handling.
β Incorrect
Checked exceptions require explicit handling or declaration, while unchecked exceptions (RuntimeExceptions) do not.
π§ Debug
advancedIdentify the error in custom exception throwing
What error will this code produce when compiled?
Java
class MyException extends Exception { public MyException(String message) { super(message); } } public class Test { public static void check(int num) { if (num < 0) { throw new MyException("Negative number: " + num); } } }
Attempts:
2 left
π‘ Hint
Check if the method declares the exception it throws.
β Incorrect
The method throws a checked exception but does not declare it with 'throws', causing a compile error.
π Syntax
advancedCorrect syntax for throwing a custom exception
Which option shows the correct way to throw a custom checked exception in Java?
Attempts:
2 left
π‘ Hint
Remember how to create new objects in Java.
β Incorrect
You must use 'new' and provide parentheses with any required arguments when throwing exceptions.
π Application
expertResult of nested custom exception handling
What will be printed when this Java program runs?
Java
class CustomException extends Exception { public CustomException(String msg) { super(msg); } } public class Main { public static void methodA() throws CustomException { throw new CustomException("Error in methodA"); } public static void methodB() { try { methodA(); } catch (CustomException e) { System.out.println("Caught in methodB: " + e.getMessage()); throw new RuntimeException("Runtime in methodB"); } } public static void main(String[] args) { try { methodB(); } catch (RuntimeException e) { System.out.println("Caught in main: " + e.getMessage()); } } }
Attempts:
2 left
π‘ Hint
Follow the flow of exceptions through the try-catch blocks.
β Incorrect
methodA throws CustomException caught in methodB, which then throws RuntimeException caught in main, printing both messages.
