This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Suppressed Exceptions  RSS feed

 
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have Just studied this topic and for what I understood is that the close() method can throw an exception in addition to the try block itself.  Also, these exceptions are saved in an suppressed array.

By doing this test, I got what I expected in the output:


Output:

But when I remove one object declaration of the <try-with-resources> for some reason, the "finally" is printed first and I just can not understand why


I thought the output will be:

Hope you can help me!
 
Mario Segovia
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry,

I thought this would be the output:


try
suppresed: java.io.IOException: closing1
finally
 
Saloon Keeper
Posts: 9997
208
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run the second version of your code I get:
 
Mario Segovia
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Stephan van Hulst, turns out that if in the print statement you use out instead of err the output is the expected. Do not know why if you keep two objects in the try-with-resources and the err in the print statement it works fine.
 
Stephan van Hulst
Saloon Keeper
Posts: 9997
208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah I see.

The reason is that when you write to two different streams, it is not guaranteed that strings written to one stream first are flushed to the output device before strings written to another stream.

If you want strings to appear in the order that you print them in, you must always print them to the same stream.
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!