• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is suppressed exception and how to get those exception in this try with resource program ?  RSS feed

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can anyone please tell me what is suppressed exception, possible furnish a resource and how can I get suppressed exceptions of this program ?
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:
Can anyone please tell me what is suppressed exception, possible furnish a resource and how can I get suppressed exceptions of this program ?


Suppressed exceptions are those exceptions thrown by the app but ignored.

If you compile your code with "javac -Xlint:all ..." you may get a whole bunch suggestions/warnings, one of these is suppressed warnings.

http://howtodoinjava.com/2013/03/13/suppressed-exceptions-in-java-7/ gives an overview and some examples about suppressed exceptions.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through the Java™ Tutorials, too.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, the first one is good one
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:


Please don't write code like this. Try-with-resources automatically closes your resources, and handles the exceptions that may occur during closing. Also, if an exception is thrown when inputStream is closed, outputStream will never be closed. The following code is more clear and robust:

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Stephan do you mean to say that I don't need try, catch and finally block inside try with resources block ?
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not for the purpose of operating with the resources. You may still need nested try statements if you're performing another operation that may throw an exception, and you want to handle the exception and continue running the outer try clause.

However, a nested try statement without any other statements in the same scope is always useless.
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For instance, let's say I want to read a file that has a number of records. The library that I use to read the records throws an exception if a record is corrupt. When I detect a corrupt record, I would still like to go on and try reading the rest of the records. Here's how I would do it:
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice explanation Stephen thank you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!