aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Extending Sun's/Oracle's interface - IOException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Extending Sun Watch "Extending Sun New topic
Author

Extending Sun's/Oracle's interface - IOException

Rafal Kawecki
Ranch Hand

Joined: Feb 08, 2011
Posts: 30

Hello,

I've been thinking of how to deal with the IOException in my application and here are my thoughts.

I decided to implement a cache in the Data class. Additionally (encouraged by some people at this forum) I extended Sun's DB interafce and provided two more methods:
  • init() - will be called to initialize cache (MAP<Integer, Room>)
  • exit() - will be called when application(server or standalone mode) terminates and will save records stored in the cache back in the database file

  • All the CRUD methods operate on the cache only so there is no problem of the IOException. Thanks to this solution I do not breake the contract of the Sun's interface.

    I was looking for an information how people deal with that situation and found an interesting post here.
    Max Habibi wrote:If you interpret the requirements religiously, then maybe Sun is trying to give you a hint on the implementation they'd like to see here. That is, maybe they're telling you there shouldn't be any IO in this method. And if that's true, maybe they expect you to add the record logically. That is, maybe they expected you to cache the db in memory, and simply add/remove from that store: thus, there's no IO. Then again, maybe not. This interpretation has it's own consequences. For example, when are you supposed to reconcile the cache with the actual file? When you exit? maybe, but it sounds like a lot to intuit from a single method's signature

    Simple scenario:
  • an examiner calls create(record) and gets IllegalStateException with a message: DB not initilized, call init() first
  • so she/he looks through the documentation and sees that that there is an init() throws IOException declared in an interface DBUrlyBird extends DB
  • he also sees that Data implements DBUrlyBird is a singleton and the docs provide the information how to get an instace
  • while trying to call init() the compiler error is raised: IOException bla bla ...

  • What do you think of that solution ? Probably there is a lot of ways to resolve this issue but will this one be acceptable ?

    I didn't find anyone who used this approach.

    Thanks
    RK
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5398
        
      13

    When my init-method fails, because of an IOException I throw a (checked) DBException. And I passed, so nothing wrong with such approach


    SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
    http://www.javaroe.be/
    Rafal Kawecki
    Ranch Hand

    Joined: Feb 08, 2011
    Posts: 30

    wow, great man

    this solution seems to be clear and nice. Thanks
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5398
        
      13

    There is a reason for throwing a generic DBException: your interface should hide implementation details. If you throw an IOException it seems you are using file I/O. And what if you have to create an implementation of your interface for a RDBMS, like MySQL. You probably have to deal with SQLExceptions (instead of IOExceptions). Using this DBException solves this issue in a clean nice way.
    Rafal Kawecki
    Ranch Hand

    Joined: Feb 08, 2011
    Posts: 30

    That's right. The scenario I provided was just an example. I was rather thinking of checked exceptions (not specifically of IOException). Anyway, thanks for pointing this out. That's is a very useful infomration for all of us.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Extending Sun's/Oracle's interface - IOException