CMSC 131 (Summer 2019) Week 08 Study Questions Flashcards
What is the output for the following program?
public class Nonsense {
public static void foo(int x) { if (x == 2) { throw new NullPointerException(); } else if (x == 3) { throw new NumberFormatException(); } System.out.println("A"); }
public static void main(String[] args) { for (int i = 1; i <= 3; i++) { try { foo(i); System.out.println("B"); } catch (NullPointerException e) { System.out.println("C"); } finally { System.out.println("D"); } System.out.println("E"); } System.out.println("F"); } }
A B D E C D E D
Where can you use a “continue” statement?
Inside of any loop.
Describe how a continue statement behaves in a while loop.
Goes to top of loop immediately and checks the boolean condition to see if looping should continue.
Describe how a continue statement works in a for-loop.
It will execute the statement that is usually processed at the end of an iteration through the loop, and then check the boolean condition to see if looping should continue.
Where can you use a “break” statement?
Either in a switch statement or in a loop. (We will learn about “switch” statements later.)
Describe how a break statement behaves.
Causes the inner-most loop or switch statement to be exited immediately.
Write some faulty code that generates a null-pointer exception, catch the exception immediately and print out “exception caught” in your catch block.
String s = null;
try {
int x = s.length(); // throws exception
} catch (NullPointerException e) {
System.out.println(“exception caught”);
}
Write some code that prompts the user for a numerical value, and reads their input into an int variable. Run the program, and try entering some text (like “cat”) instead of a number. Notice what kind of exception is thrown. Now modify your program so that it catches this exception, and instead of crashing the program, have it tell the user that he/she must enter a NUMBER, and then prompt them for input again.
Scanner s = new Scanner(System.in);
boolean goodNumberEntered = false; while (true) { System.out.println("enter a number: "); try { int n = s.nextInt(); } catch(java.util.InputMismatchException e) { s.nextLine(); //removes previous input from stream System.out.println("No, you MUST ENTER A NUMBER!"); continue; } break; }
Write a method called smallSum that takes two int parameters, x and y. If the absolute value of the sum of the integers is more than 100, throw an ArithmeticException, passing the String “I don’t like big numbers” to the constructor of the exception. If the sum is less than 100, then return the sum. Write a quick driver to test out your method. After making sure everything works correctly, modify the driver so that it catches the exception and prints out the message that was passed to the exception’s constructor, but doesn’t crash the program.
public static int smallSum(int x, int y) { if (Math.abs(x + y) > 100) { throw new ArithmeticException("I don't like big numbers!"); } return x + y; }
public void static main(String[] args) { try { smallSum(50, 60); } catch (ArithmeticException e) { System.out.println(e.getMessage()); } }
Explain the relationship between exception handling and the call stack. What happens if an exception is thrown but not caught anywhere in your program?
When an exception is thrown, Java looks for an appropriate catch block in the place where the problem occurred. If a catch is not found there, then Java discards the current frame on the call stack, and looks for an appropriate catch block in the previous frame. If none is found, that frame is discarded, etc. If the exception is not caught anywhere, then the program terminates, and the exception propagates to the “outside world”, which for us is the Eclipse IDE. Eclipse displays that red and blue error notification with stack trace in the console.
Under what circumstances will the finally block run?
As long as the “try” block has been entered, the finally block will run (no matter how you leave!)