Try
suppressed:java.io.IOException: Closing
Closing - 2
suppressed:java.io.IOException: Closing - 1
Which exceptions will the code throw?
- RuntimeException c with no suppressed exception
Whic exceptions will the code throw?
- IOException with suppressed RuntimeException a
Try
suppressed:java.io.IOException: Closing
Closing - 2
suppressed:java.io.IOException: Closing - 1
in the second example there are two resources to be closed and is closed before b2 and then b1 but because you get that ouput?
Can you explain the operation?
BSc Software Engineering (Hons), OCP 6, OCP 7, OCP 8
https://twitter.com/jaymoid
BSc Software Engineering (Hons), OCP 6, OCP 7, OCP 8
https://twitter.com/jaymoid
John Lerry wrote:Then an exception is said suppressed at the time when it can not be handled by a catch block. Is it correct?
John Lerry wrote:The first example is clear to me and also the second it is, in particular, the exception throw by the closing of b2 is not suppressed because it is managed by the catch block of the main.
BSc Software Engineering (Hons), OCP 6, OCP 7, OCP 8
https://twitter.com/jaymoid
James Pittendreigh wrote:
I kinda think that the question is a little ambiguous, maybe it should be: "Which exceptions will the Animals.run() method throw?"
John Lerry wrote:
James Pittendreigh wrote:
I kinda think that the question is a little ambiguous, maybe it should be: "Which exceptions will the Animals.run() method throw?"
Then should I not consider the exception throws in the close() method (exception suppressed)?
John Lerry wrote:I ask this because it seems that that exception in the exercise #11 is considered and it is present in the result as an exception suppressed.
BSc Software Engineering (Hons), OCP 6, OCP 7, OCP 8
https://twitter.com/jaymoid
James Pittendreigh wrote:
Correct. You could think of the question as: "If you compiled and ran that section of code, which exception(s) would be thrown", and only the RuntimeException C would be thrown. As I say, I think it is a little ambiguous, but I also found questions on my real 6 exam ambiguous.
John Lerry wrote:
#11
Which exceptions will the code throw?
- IOException with suppressed RuntimeException a
John Lerry wrote:
ok, but then the exercise # 11
...
should give as ouput "IOException with no suppressed exception" but that is not what the correct ouput for the book.
James Pittendreigh wrote: The Try block throws the IOException and becomes the main exception (as it is the first), then when the resources are closed implicitly - the RuntimeException is added as a Suppressed exception to the main (IOException) Exception. This is caught in the Exception block and then thrown again.
- catch
- An ArrayIndexOutOfBoundsException is thrown
- finally
- An ArrayIndexOutOfBoundsException is thrown
BSc Software Engineering (Hons), OCP 6, OCP 7, OCP 8
https://twitter.com/jaymoid
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |