Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX:IOExceptions What to do?

 
Tony Collins
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11914
209
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tony Collins
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11914
209
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11914
209
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic