0
0
Javaprogramming~20 mins

Creating custom exception class in Java - Practice Exercises

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 message
What is the output of this Java program that uses a custom exception class?
Java
class MyException extends Exception {
    public MyException(String message) {
        super(message);
    }
}

public class Test {
    public static void main(String[] args) {
        try {
            throw new MyException("Custom error occurred");
        } catch (MyException e) {
            System.out.println(e.getMessage());
        }
    }
}
ACustom error occurred
BMyException: Custom error occurred
CCompilation error
DException in thread "main" MyException: Custom error occurred
Attempts:
2 left
πŸ’‘ Hint
Look at what getMessage() returns from the Exception class.
❓ Predict Output
intermediate
2:00remaining
Value of variable after catching custom exception
What is the value of variable 'result' after running this code?
Java
class MyException extends Exception {}

public class Test {
    public static int testMethod() throws MyException {
        throw new MyException();
    }

    public static void main(String[] args) {
        int result = 0;
        try {
            result = testMethod();
        } catch (MyException e) {
            result = -1;
        }
        System.out.println(result);
    }
}
A0
B-1
CCompilation error
DException in thread "main" java.lang.MyException
Attempts:
2 left
πŸ’‘ Hint
Consider what happens when the exception is thrown and caught.
πŸ“ Syntax
advanced
2:00remaining
Identify syntax error in custom exception class
Which option contains a syntax error in defining a custom exception class?
A
public class MyException extends Exception {
    public MyException() {}
}
B
public class MyException extends Exception {
    public MyException() {
        super();
    }
}
C
public class MyException extends Exception {
    public MyException(String message) {
        super(message);
    }
}
D
public class MyException extends Exception {
    public MyException(String message) {
        this.message = message;
    }
}
Attempts:
2 left
πŸ’‘ Hint
Check how the superclass Exception stores the message.
🧠 Conceptual
advanced
2:00remaining
Purpose of creating a custom exception class
Why would a developer create a custom exception class instead of using existing Java exceptions?
ATo avoid using try-catch blocks in the code
BTo automatically fix errors during runtime
CTo provide more specific error information related to the application domain
DTo improve the performance of exception handling
Attempts:
2 left
πŸ’‘ Hint
Think about how exceptions help communicate problems.
πŸ”§ Debug
expert
2:00remaining
Why does this custom exception code fail to compile?
This code tries to define a custom checked exception but fails to compile. What is the cause?
Java
class MyException extends Exception {
    public MyException() {
        System.out.println("Error");
    }
}

public class Test {
    public static void main(String[] args) {
        throw new MyException();
    }
}
AYou cannot throw checked exceptions without declaring them in method signature
BMyException constructor does not call super(), causing a compile error
CThe catch block must catch Exception, not MyException
DSystem.out.println cannot be used inside exception constructors
Attempts:
2 left
πŸ’‘ Hint
Check the method main's throws declaration and checked exceptions rules.