Write and implement two classes: MightBeFixableException must be a checked excep
ID: 3873835 • Letter: W
Question
Write and implement two classes:
MightBeFixableException must be a checked exception. It needs a constructor. The constructor should have a single String parameter. Your constructor should chain to the superclass constructor using the command super(parameterName); (where parameterName is replaced with the constructor parameter's name) to set the exception's message.
FixesNotExpectedException must be an unchecked exception. It must define a single constructor that does not have any parameters. The constructor must start with the command super("TIFU: This exception was raised."); to set the exception's message.
Here are the Junit Test cases that test these two classes:
package homework;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.junit.Test;
/**
* Test cases for your classes defining important methods.
*/
public class RaiseExceptionTest {
@Test
public void testProbablyUnfixableExceptionClassDefinition() {
try {
RaiseException.simpleMethodRaisingException(2);
fail("Should have thrown an exception when makeException(2) called!");
} catch (Exception e) {
assertTrue("When makeException(2) called, you should be throwing the exception that DOES NOT require being listed in a throws clause!",
e instanceof FixesNotExpectedException);
assertTrue("When makeException(2) called, you should be throwing the exception that DOES NOT require being listed in a throws clause!",
e instanceof RuntimeException);
assertEquals("Incorrect message set in ProbablyUnfixableException constructor",
"TIFU: This exception was raised.", e.getMessage());
}
try {
int answer = RaiseException.simpleMethodRaisingException(0);
assertEquals(42, answer);
} catch (MightBeFixableException e) {
fail("When makeException(0) called, your method should return 42!");
}
}
@Test
public void testprobablyUnfixableExceptionClassMembers() {
Field[] hosedFields = FixesNotExpectedException.class.getDeclaredFields();
assertEquals("ProbablyUnfixableException should not declare any fields!", 0, hosedFields.length);
Method[] hosedMethods = FixesNotExpectedException.class.getDeclaredMethods();
assertEquals("ProbablyUnfixableException should not define any methods.", 0, hosedMethods.length);
Constructor<?>[] hosedConstructors = FixesNotExpectedException.class.getConstructors();
assertEquals("ProbablyUnfixableException should only define exactly 1 constructor.", 1, hosedConstructors.length);
}
@Test
public void testFixItExceptionClassDefinition() {
try {
RaiseException.simpleMethodRaisingException(-4982);
fail("Should have thrown an exception when makeException(-4982) called!");
} catch (Exception e) {
assertTrue("When makeException(-4982) called, you should be throwing the exception that DOES require being listed in a throws clause!",
e instanceof MightBeFixableException);
assertTrue("When makeException(-4982) called, you should be throwing the exception that DOES require being listed in a throws clause!",
e instanceof Exception);
assertFalse("When makeException(-4982) called, you should be throwing the exception that DOES require being listed in a throws clause!",
e instanceof RuntimeException);
assertEquals("Incorrect message set in FixItNowException constructor",
"I just learned this; Exception is checked; I will raise it; Fix bug, maybe?", e.getMessage());
}
try {
int answer = RaiseException.simpleMethodRaisingException(0);
assertEquals(42, answer);
} catch (MightBeFixableException e) {
fail("When makeException(0) called, your method should return 42!");
}
}
@Test
public void testFixItExceptionClassMembers() {
Field[] fixItFields = MightBeFixableException.class.getDeclaredFields();
assertEquals("FixItNowException should not declare any fields!", 0, fixItFields.length);
Method[] fixItMethods = MightBeFixableException.class.getDeclaredMethods();
assertEquals("FixItNowException should not define any methods.", 0, fixItMethods.length);
Constructor<?>[] fixItConstructors = MightBeFixableException.class.getConstructors();
assertEquals("FixItNowException should only define exactly 1 constructor.", 1, fixItConstructors.length);
}
}
Explanation / Answer
Solution=================
Here are the classes reqd:
//Checked Exception, by extending Exception class
class MightBeFixableException extends Exception{
public MightBeFixableException(String parameter){
super(parameter);
}
}
//Unchecked Exception, by extending RuntimeException class
class FixesNotExpectedException extends RuntimeException{
public FixesNotExpectedException(){
super("TIFU: This exception was raised.");
}
}
However, I was not able to test it, because in the provided code did not had the definintion for the class named "RaiseException" and it's subsequent method simpleMethodRaisingException..
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.