Ruslan Rakhmedov

Greenhorn
+ Follow
since Jun 07, 2019
Ruslan likes ...
IntelliJ IDE Java Oracle
Russian Federation
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
4
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ruslan Rakhmedov

Chapter 6 ■ Exceptions and Assertions
Exam Essentials topic:
"Determine if an exception is checked or unchecked. Checked exceptions
are in the Exception class hierarchy but not the RuntimeException hierarchy.
DateTimeParseException, IOException, and SQLException are common checked
exceptions."

Here DateTimeParseException also determinates as checked exception, but it is not
Chapter 6 ■ Exceptions and Assertions
Rethrowing Exceptions topic:
"It is a common pattern to log and then throw the same exception. Suppose that we have a
method that declares two checked exceptions:
public void parseData() throws SQLException, DateTimeParseException {}"
but DateTimeParseException is unchecked exception
Thank you. But i still think that performCount() can't throw any exception, it can throw only unchecked one.
21. Assuming an implementation of the performCount() method is provided prior to runtime,
which of the following are possible results of executing the following application? (Choose
all that apply.)

import java.util.*;
import java.util.concurrent.*;
import java.util.stream.*;

public class CountZooAnimals
{
public static Integer performCount(int exhibitNumber)
{
// IMPLEMENTATION OMITTED
}

public static void printResults(Future<?> f)
{
try
{
System.out.println(f.get()); // o1
}
catch (Exception e)
{
System.out.println("Exception!");
}
}

public static void main(String[] args) throws InterruptedException,
ExecutionException
{
ExecutorService service = Executors.newSingleThreadExecutor();
final List<Future<?>> results = new ArrayList<>();
IntStream.range(0, 10)
.forEach(i -> results.add(
service.submit(() -> performCount(i)))); // o2
results.stream().forEach(f -> printResults(f));
service.shutdown();
}
}

A. It outputs a number 10 times.
B. It outputs a Boolean value 10 times.
C. It outputs a null value 10 times.
D. It outputs Exception! 10 times.
E. It hangs indefinitely at runtime.
F. It throws an unhandled exception at runtime.
G. The code will not compile because of line o1.
H. The code will not compile because of line o2.

and answers

21. A, C, D, E. The code compiles and runs without issue, so G and H are incorrect. The return type of performCount() is Integer, so the submit() is interpreted as being applied to a
Callable<Integer> value. In this manner, the Future<?> is really a Future<Integer> object. One possible implementation of performCount() is just to return the input parameter;
therefore A is a correct answer. B is incorrect, because the return type is Integer, not Boolean. The performCount() method could just return null, so C is a correct choice.
The performCount() can also throw a runtime exception; therefore D is also a correct answer. It is also possible for our performCount() to hang indefinitely, such as in a deadlock.
This would cause Future.get() to hang in printResults(), making E also a correct answer. Finally, any exception thrown in performCount() will appear as an exception in the get() operation.
Since the get() operations are caught in a try/catch block inprintResults(), none of them will be unhandled, and F is incorrect.
The book what i'm talking about is "Oracle Certified Professional Java SE 8 Programmer II " by Jeanne Boyarsky and Scott Selikoff ISBN: 978-1-119-06790-0
And yes, i'm already think about unchecked exceptions.
But then, anwser to the question 21 in "Answer appendix on Chapter 7: Concurrency" need specify that it means unchecked exceptions
"...Finally, any exception thrown in performCount() will appear as an exception
in the get() operation."
Method performCount does't have any checked exception in throws section.
So if we try to throw any checked exception in body of method, application would't compile.
Correct me if i'm wrong.

And sorry for my English. Thank you.