wood burning stoves*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Throwing exception on closing the data file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Throwing exception on closing the data file" Watch "Throwing exception on closing the data file" New topic
Author

Throwing exception on closing the data file

Kevin Broderick
Ranch Hand

Joined: Jul 19, 2009
Posts: 39
Hi All,

I don't know what to do here. As with all exceptions being raised inside the data class, they are all thrown up to the gui, but there is this situation where on writing back out to the file I must handle an IOException. I call on writing the file when I'm closing down the application so I cannot propagate it up to the gui. I suppose it would be right to catch the exception and use JOptionPane message informing the user of whatever went wrong on writing the file inside the data class. I believe that this is the way to do it but I'm just writing this here in case any of you ranchers here have a better way.

Cheers in advance

Kevin.
David Byron
Rancher

Joined: Jan 20, 2009
Posts: 171

Kevin Broderick wrote:...there is this situation where on writing back out to the file I must handle an IOException. I call on writing the file when I'm closing down the application so I cannot propagate it up to the gui. I suppose it would be right to catch the exception and use JOptionPane message informing the user of whatever went wrong on writing the file inside the data class. I believe that this is the way to do it....


If you're storing the full database in memory and operating only on your cache until the user closes the app, then an error indicating that the file could not be written should be propagated all the way to the GUI. It should declare in the boldest way possible that all transactions that have occurred in that session are about to be blown into the atmosphere because of an I/O problem and that shutdown has been aborted!

On the other hand, if you've successfully written to the file whatever ought to be written, and you're just closing the file, you have to decide whether to (a) attempt to handle the abnormal termination (say, with a retry) or (b) swallow the error (in a self-documenting way) as inconsequential if you're quite sure it is inconsequential.

Whatever you do, be sure to explain it in your decisions doc.


SCJD 6, OCPJP7, Baroque Potion, G+
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
How about holding the GUI open until the I/O completes?

Jim ... ...


BEE MBA PMP SCJP-6
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Kevin,


I defined my own DataAccessException a wrapper class, which exists only to wrap low-level
exceptions specific to each storage mechanism (for example, SQLException and
IOException). When an implementation class throws an exception, it is caught,
wrapped in a DataAccessException, and then rethrown. This protects the
business layer from ripple effects caused by changes to the Data
class implementation.


HTH,


Carlos.


SCSA, OCA, SCJP 5.0, SCJD http://www.linkedin.com/in/carlosamorillo
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

Hi Kevin,

It all depends when the writing back occurs. If you use a shutdownhook there is not a lot you can do, because it is a part of the termination process of the JVM. So logging is almost the only thing you can do.
When you have yourself complete control about the application (with a gui), you can call a method yourself and be able to show any possible error before exiting.

In my application I also used the shutdownhook, so I documented in choices.txt some alternative solutions to prevent minimal data loss to implement in future releases and that's it.

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Throwing exception on closing the data file
 
Similar Threads
BufferedWriter problems
Why must we close BufferedWriter when finish writing?
B&S ConfigurationGUI DB location process/errorhandling in init
Looking for some nice advice
Where do I put bus. logic for GUI class?