aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX:IOExceptions What to do? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NX:IOExceptions What to do?" Watch "NX:IOExceptions What to do?" New topic
Author

NX:IOExceptions What to do?

Tony Collins
Ranch Hand

Joined: Jul 03, 2003
Posts: 435
I have a static class block in my Data class to read the db schema and load the cache with it's initial records. My problem is what to do If I receive an IO exception/FileNotFoundException reading from the db file.
Stop the Server or just log a message. Any ideas ?
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11280
    
  59

Hi Tony,
Do you have a GUI for your server? Can you display a message telling the user what is wrong? Can you prompt them for the location of a valid database file?
In my opinion, if you cannot get some sort of fix from the user, then you may as well shut down the server because it is not going to provide anything to the clients. If you can get a fix from the user, then prompt them for the fix, then try again.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Tony Collins
Ranch Hand

Joined: Jul 03, 2003
Posts: 435
But when a static intiliser block throws an Exception whom is it caught by ?
Is a static block a bad choice to set up the initial cache in then ?
Cheers
Tony
[ July 28, 2003: Message edited by: Tony Collins ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11280
    
  59

Hi Tony,
Your class still has to be created doesn't it? So you can catch the exceptions from the static block when you create the instance of the class.
Regards, Andrew
Tony Collins
Ranch Hand

Joined: Jul 03, 2003
Posts: 435
I changed the static block to a static method(startServer) as I understand the static block, is called when the class is loaded into the JVM not when an instance is created. I have control of when the class is initilised then.
I have read several posts regarding traping IOExceptions( which I would think would never happen anyway) wondered if anyone agreed that IO Exceptions should really be dealt with in the method that throws them. As actions like closing files down should be carried out at that point, maybe faliure of server to intilise could be reported by a more general Exception.
Tony
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11280
    
  59

Hi Tony,
Sorry, wasn't thinking clearly last night. The Java Language Specification tells us that static initializers cannot throw checked exceptions:
Static initializers (�8.7), class variable initializers, and instance initializers or instance variable initializers within named classes and interfaces (�8.3.2), must not result in a checked exception; if one does, a compile-time error occurs.

So you cannot throw the IOException or FileNotFoundException from the static initializer.
Regards, Andrew
[ July 28, 2003: Message edited by: Andrew Monkhouse ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: NX:IOExceptions What to do?
 
Similar Threads
What does it mean by " Take a delimited ASCII file data and turn it into binary file"
B&S DB interface - should I use it?
Reading fields' information of DataBase
H2 database locking
"Fatal exception occurred. Program will exit."