0
0
Javaprogramming~20 mins

Why custom exceptions are needed in Java - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Custom Exception Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of Custom Exceptions in Java
Why do developers create custom exceptions instead of using only built-in exceptions?
ATo provide more specific error information related to the application domain
BBecause Java does not have any built-in exceptions
CTo make the program run faster by avoiding built-in exceptions
DTo reduce the size of the compiled Java program
Attempts:
2 left
πŸ’‘ Hint
Think about how custom exceptions help in understanding errors better.
❓ Predict Output
intermediate
2:00remaining
Output of Custom Exception Handling
What will be the output of this Java code?
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 not allowed");
        } else {
            System.out.println("Number is " + num);
        }
    }

    public static void main(String[] args) {
        try {
            check(-5);
        } catch (MyException e) {
            System.out.println(e.getMessage());
        }
    }
}
ARuntime error: NullPointerException
BNumber is -5
CNegative number not allowed
DCompilation error due to missing exception handling
Attempts:
2 left
πŸ’‘ Hint
Look at what happens when num is less than zero.
πŸ”§ Debug
advanced
2:00remaining
Identify the Error in Custom Exception Usage
What error will this Java 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");
        }
    }
}
ARuntime error: MyException thrown but not caught
BCompilation error: unhandled exception MyException
CNo error, code compiles and runs fine
DCompilation error: MyException class not found
Attempts:
2 left
πŸ’‘ Hint
Check if the method declares the exception it throws.
πŸ“ Syntax
advanced
2:00remaining
Correct Syntax to Define a Custom Exception
Which option shows the correct way to define a custom checked exception in Java?
A
class MyException extends RuntimeException {
    public MyException(String msg) {
        super(msg);
    }
}
B
class MyException {
    public MyException(String msg) {
        super(msg);
    }
}
C
class MyException extends Throwable {
    public MyException(String msg) {
        super(msg);
    }
}
D
class MyException extends Exception {
    public MyException(String msg) {
        super(msg);
    }
}
Attempts:
2 left
πŸ’‘ Hint
Checked exceptions must extend Exception but not RuntimeException.
πŸš€ Application
expert
3:00remaining
Custom Exception Usage in Application Logic
Given this scenario: You want to signal a specific error when a user tries to withdraw more money than their account balance. Which custom exception usage is best to handle this?
ACreate a custom checked exception 'InsufficientFundsException' and throw it when withdrawal amount exceeds balance
BThrow a generic Exception with a message 'Insufficient funds' without creating a custom exception
CUse built-in NullPointerException to indicate insufficient funds
DUse RuntimeException without a custom class to signal insufficient funds
Attempts:
2 left
πŸ’‘ Hint
Think about clarity and forcing the caller to handle the error.