0
0
Javaprogramming~10 mins

Creating custom exception class in Java - Interactive 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'
AException
BString
CError
DObject
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Extending String or Object instead of Exception.
Extending Error which is for serious JVM errors.
2fill in blank
medium

Complete the constructor to pass the error message to the superclass.

Java
public class MyException extends Exception {
    public MyException([1] message) {
        super(message);
    }
}
Drag options to blanks, or click blank then click option'
AString
Bint
Cboolean
Ddouble
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using int or boolean instead of String for the message parameter.
3fill in blank
hard

Fix the error in the constructor to correctly call the superclass constructor.

Java
public class MyException extends Exception {
    public MyException(String message) {
        [1](message);
    }
}
Drag options to blanks, or click blank then click option'
Athis
BMyException
Csuper
DException
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'this' instead of 'super' to call the parent constructor.
Calling the class name as a method.
4fill in blank
hard

Fill both blanks to add a no-argument constructor that calls the superclass constructor with a default message.

Java
public class MyException extends Exception {
    public MyException() {
        [1]("[2] error occurred");
    }
}
Drag options to blanks, or click blank then click option'
Asuper
Bthis
CCustom
DDefault
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'this' instead of 'super' in the constructor.
Using an unclear or incorrect default message.
5fill in blank
hard

Fill all three blanks to add a constructor that accepts a message and a cause, passing both to the superclass.

Java
public class MyException extends Exception {
    public MyException([1] message, [2] cause) {
        [3](message, cause);
    }
}
Drag options to blanks, or click blank then click option'
AString
BThrowable
Csuper
DException
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using wrong types for message or cause.
Not calling the superclass constructor properly.