0
0
Javaprogramming~10 mins

Throwing custom exceptions in Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a custom exception class named MyException.

Java
public class MyException extends [1] {
}
Drag options to blanks, or click blank then click option'
ARuntimeException
BString
CExceptionHandler
Dint
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Extending a non-exception class like String or int.
Forgetting to extend any class.
2fill in blank
medium

Complete the constructor of the custom exception to accept a message.

Java
public class MyException extends RuntimeException {
    public MyException([1] String message) {
        super(message);
    }
}
Drag options to blanks, or click blank then click option'
Aprivate
Bfinal
Cpublic
Dstatic
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using access modifiers like private or public on parameters.
Omitting the parameter type.
3fill in blank
hard

Fix the error in throwing the custom exception with a message.

Java
throw new MyException([1]);
Drag options to blanks, or click blank then click option'
Anew MyException()
BException
C"Error occurred"
DMyException
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Passing the class name instead of a message.
Not passing any argument.
4fill in blank
hard

Fill both blanks to throw a custom exception when a number is negative.

Java
if (number [1] 0) {
    throw new MyException([2]);
}
Drag options to blanks, or click blank then click option'
A<
B"Negative number not allowed"
C>
D"Number is positive"
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using > instead of < for the condition.
Throwing exception with wrong message.
5fill in blank
hard

Fill all three blanks to define and throw a custom exception with a message including the invalid value.

Java
public class MyException extends RuntimeException {
    public MyException(String message) {
        super(message);
    }
}

int value = -5;
if (value [1] 0) {
    throw new MyException("Invalid value: " + [2] + [3]);
}
Drag options to blanks, or click blank then click option'
A<
Bvalue
C"!"
D0
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using > instead of < in the condition.
Not concatenating the message parts correctly.