| 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: 10826
|
|
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: 10826
|
|
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: 10826
|
|
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 ]
|
 |
 |
|
|
subject: NX:IOExceptions What to do?
|
|
|