exam questions

Exam 1z0-809 All Questions

View all questions & answers for the 1z0-809 exam

Exam 1z0-809 topic 1 question 14 discussion

Actual exam question from Oracle's 1z0-809
Question #: 14
Topic #: 1
[All 1z0-809 Questions]

Given the code fragments:
4. void doStuff() throws ArithmeticException, NumberFormatException, Exception {
5. if (Math.random() >-1 throw new Exception ("Try again");
6. }
and
24. try {
25. doStuff ( ):
26. } catch (ArithmeticException | NumberFormatException | Exception e) {
27. System.out.println (e.getMessage()); }
28. catch (Exception e) {
29. System.out.println (e.getMessage()); }
30. }
Which modification enables the code to print Try again?

  • A. Comment the lines 28, 29 and 30.
  • B. Replace line 26 with: } catch (Exception | ArithmeticException | NumberFormatException e) {
  • C. Replace line 26 with: } catch (ArithmeticException | NumberFormatException e) {
  • D. Replace line 27 with: throw e;
Show Suggested Answer Hide Answer
Suggested Answer: C 🗳️

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
asdfjhfgjuaDCV
4 months, 4 weeks ago
C is the correct answer. public class Test { static void doStuff() throws ArithmeticException, NumberFormatException, Exception { if (Math.random() > -1) throw new Exception("Try again"); } public static void main(String[] args) { try { doStuff(); } catch (ArithmeticException | NumberFormatException e) { System.out.println(e.getMessage()); } catch (Exception e){ System.out.println(e.getMessage()); } } }
upvoted 1 times
...
steefaand
5 months, 4 weeks ago
Selected Answer: C
C is correct since Exception classes in multi-catch must be unrelated. Also if superclass and sublass are caught then code must first catch subclass and then superclass.
upvoted 1 times
...
r1muka5
1 year, 5 months ago
Option C is correct despite the fact that the code wouldnt still compile because of error on line 5 (missing closing bracket in the if)
upvoted 1 times
...
WilsonKKerll
2 years, 4 months ago
Option A can't compile Types in multi-catch must be disjoint: 'java.lang.NumberFormatException' is a subclass of 'java.lang.Exception'
upvoted 1 times
...
WilsonKKerll
2 years, 5 months ago
Answer C is better than A. Code is more readable.
upvoted 1 times
mevlt
2 years, 4 months ago
it is not just because of readability. multi-catch can't have parent and child together. Exception is parent of both ArithmeticException and NumberFormatException.
upvoted 3 times
...
...
bnagaraja9099
3 years, 3 months ago
Ans: C. If you do A, you still get this exception The exception ArithmeticException is already caught by the alternative Exception
upvoted 4 times
...
pul26
3 years, 7 months ago
Answer is C
upvoted 3 times
...
varconite
3 years, 8 months ago
Answer is C
upvoted 2 times
...
luzeli25
3 years, 10 months ago
A is correct
upvoted 2 times
...
Innovation
4 years, 6 months ago
C is correct
upvoted 3 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago