Complete the code to declare a custom exception class named MyException.
public class MyException extends [1] { }
The custom exception class should extend RuntimeException or Exception. Here, RuntimeException is used.
Complete the constructor of the custom exception to accept a message.
public class MyException extends RuntimeException { public MyException([1] String message) { super(message); } }
The constructor parameter can be declared final to prevent modification, but it's optional. Here, final is used as a good practice.
Fix the error in throwing the custom exception with a message.
throw new MyException([1]);When throwing the exception, you must pass a string message to the constructor, like "Error occurred".
Fill both blanks to throw a custom exception when a number is negative.
if (number [1] 0) { throw new MyException([2]); }
The condition checks if the number is less than zero, then throws the exception with the message "Negative number not allowed".
Fill all three blanks to define and throw a custom exception with a message including the invalid value.
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]); }
The code checks if value is less than zero, then throws MyException with a message showing the invalid value followed by an exclamation mark.