• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Suppressed Exception

 
Ranch Hand
Posts: 47
IntelliJ IDE Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am reading a tutorial from webpage about supressed  exceptions and have question about correctness of code:



Shouldn't last catch be after `if ` statement? I I can have access to resource without exception, but closing it can raise one, so can be null in this catch. Hence I think this example is incorrect.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a horrible example. If the finally block does not cause an exception, the original exception is never thrown. The following would be a better version:

This not only throws an exception whenever anything went wrong, it also throws the correct exception - the one thrown from the main body, not the one thrown from the cleanup.

Of course, the following is (nearly) identical to the above but much, much cleaner:

The only thing that's different is the scope of the resource variable.
 
Andrzej Zahorski
Ranch Hand
Posts: 47
IntelliJ IDE Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank's I'm adding this website to my blacklist. BTW, do you know any good resource for Core stuff?
I searched many websites and to be honest people do tons of mistakes in them. I read:
* Official tutorials - often lacking in examples
* Jenkov tutorials - webpage - bad practices in code, some of theads examples didn't work.
* Baeldung webpage - good but there's no order in it

The best page I've seen is Journaldev: webpage

Books:
* Thinking In Java - extensive private libraries and Java 7
* Core Java by Cay Horstmann - scope of topics is often wrongly placed - for example in String API guy writes multiple pages about System.out.format - this is not crucial knowledge in programming.


 
Marshal
Posts: 79961
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrzej Zahorski wrote:. . . * Core Java by Cay Horstmann - scope of topics is often wrongly placed - for example in String API guy writes multiple pages about System.out.format - this is not crucial knowledge in programming.

Disagree. Core Java is not a beginner's book; you needn't expect the chapters to be in the order you would teach a beginner. Also, System.out.format and its brother, System.out.printf(), are used all the time. Once you start programming anything using the terminal/command line, you will use System.out.format()/printf() on every page, and you will need to know about the many combinations of % tags. So I think the coverage you are complaining about is probably correct.
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic