Back to questions
JuniorTheoryCommon
What does the program print?
public class Main {
public static void main(String[] args) {
try {
int n = Integer.parseInt("x");
System.out.print(1);
} catch (NumberFormatException e) {
System.out.print(2);
} finally {
System.out.print(3);
}
}
}23
- Expecting
123: the line that throws endstryat once, soprint(1)after it never runs; control goes straight to thecatch. - Expecting
2:finallyis not skipped when acatchhas handled the exception; it runs aftertryandcatchwhatever happens.
- What does the program print if the string is
"5"instead of"x"? - Does
finallyrun if thetryblock ends withreturn?