Hello. For my assignment, I need two working programs. One will throw a NullPoin
ID: 3602964 • Letter: H
Question
Hello. For my assignment, I need two working programs. One will throw a NullPointException and the other will Catch the NullPointException. What am I missing in these programs?
Thank you
public class NullPointerExceptionThrown {
static void NullPointerException()
{
try
{
throw new NullPointerException ("Suzie");
}
public static void main (String args [])
{
try {
NullPointerException ();
}
}
}
}
public class NullPointerExceptionCatch {
catch (NullPointerException exception) {
System.out.println("caught inside NullPointerException");
throw e;
}
catch (NullPointerException exception) {
System.out.println("Recaught: " + exception);
}
}
Explanation / Answer
NullPointerExceptionThrown
NullPointerExceptionCatch
Modified code:
public class NullPointerExceptionThrown {
NullPointerExceptionThrown() {
throw new NullPointerException ("NULL POINTER EXCEPTION");
} // constructor closed
} // class closed
public class NullPointerExceptionCatch {
NullPointerExceptionCatch (NullPointerException exception) {
System.out.println("Caught inside NullPointerException");
} // constructor closed
} // class closed
public class program{
public static void main (String args []) {
try {
NullPointerExceptionThrown e = new NullPointerExceptionThrown();
} // try closed
catch (NullPointerException ex){
NullPointerExceptionCatch c = new NullPointerExceptionCatch(ex);
} // catch closed
} // main closed
} // class closed
If this helps please give thumbs up. Happy Chegging!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.