0
0
Javaprogramming~20 mins

Throwing custom exceptions in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Custom Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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());
        }
    }
}
ACaught: Negative number: -5
BNumber is -5
CException in thread "main" MyException: Negative number: -5
DCompilation error due to missing throws declaration
Attempts:
2 left
πŸ’‘ Hint
Look at how the exception is thrown and caught in the try-catch block.
🧠 Conceptual
intermediate
1:30remaining
Understanding checked vs unchecked exceptions
Which statement correctly describes the difference between checked and unchecked exceptions in Java?
AChecked exceptions must be declared or caught; unchecked exceptions do not need to be declared or caught.
BNeither checked nor unchecked exceptions need to be declared or caught.
CBoth checked and unchecked exceptions must always be caught or declared.
DUnchecked exceptions must be declared or caught; checked exceptions do not need to be declared or caught.
Attempts:
2 left
πŸ’‘ Hint
Think about compiler requirements for exception handling.
πŸ”§ Debug
advanced
2:00remaining
Identify 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);
        }
    }
}
ACompilation error: missing constructor in MyException
BRuntime error: MyException thrown but not caught
CCompilation error: unhandled exception type MyException
DNo error, code compiles and runs fine
Attempts:
2 left
πŸ’‘ Hint
Check if the method declares the exception it throws.
πŸ“ Syntax
advanced
1:00remaining
Correct syntax for throwing a custom exception
Which option shows the correct way to throw a custom checked exception in Java?
Athrow new MyException;
Bthrow MyException("Error occurred");
Cthrow MyException;
Dthrow new MyException("Error occurred");
Attempts:
2 left
πŸ’‘ Hint
Remember how to create new objects in Java.
πŸš€ Application
expert
3:00remaining
Result 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());
        }
    }
}
ACaught in methodB: Error in methodA
B
Caught in methodB: Error in methodA
Caught in main: Runtime in methodB
CCaught in main: Runtime in methodB
DException in thread "main" java.lang.RuntimeException: Runtime in methodB
Attempts:
2 left
πŸ’‘ Hint
Follow the flow of exceptions through the try-catch blocks.