This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes IOException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "IOException" Watch "IOException" New topic
Author

IOException

Devu Shah
Ranch Hand

Joined: Jun 21, 2001
Posts: 50
Hello everybody,
can any one explain why 'IOException' is THROWN in data class ?
Data class is throing 'DatabaseException' is ok as there is class which will handle DatabaseException. BUT what is the idia behind throwing IOException (insteading of just handling there only) in Data class ?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

The data class uses IOException because it is reading from the db.db file. You pass this file and location to the data class. So if the data class handled this problem, you would not know it, and would not have any data. So it is the responsibility of the class that creates the Data class object and passes the location of the db.db file. So that it can stop the program, or alert you.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Devu Shah
Ranch Hand

Joined: Jun 21, 2001
Posts: 50
Thankx for reply.
dean tomlinson
Ranch Hand

Joined: Jan 31, 2002
Posts: 94
in many of the methods in Data, the IOException is caught and this is converted into a DatabaseException which is then returned to the user.
this is a recommended technique where the type of exception thrown from a method is relevant to the level of abstraction, of the method call.

the thing i am confused about, is that this seems to be done inconsistently, so some methods convert the IOException to the DatabaseException, others just return the IOException.
Does anyone think it is part of the assignment to tidy this side of things up, so all methods throw the DatabaseException, and the DatabaseException could be modifed so that the IOException can be chained to it ?
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
Yes. In my submission, all the methods in Data threw DatabaseException and the RMI remote object threw only RemoteException encapsulating the DatabaseException in it.
[ June 01, 2002: Message edited by: Sai Prasad ]
Steven Sloggett
Greenhorn

Joined: Feb 11, 2002
Posts: 15
Originally posted by Sai Prasad:
Yes. In my submission, all the methods in Data threw DatabaseException and the RMI remote object threw only RemoteException encapsulating the DatabaseException in it.
[ June 01, 2002: Message edited by: Sai Prasad ]

I can understand only throwing DatabaseException from Data but I'd argue that you should still throw DatabaseException from the RMI object. I'd leave RemoteExecption to be raised by the stub due to network problems.
What do others think?


Steven
dean tomlinson
Ranch Hand

Joined: Jan 31, 2002
Posts: 94
I can understand only throwing DatabaseException from Data but I'd argue that you should still throw DatabaseException from the RMI object. I'd leave RemoteExecption to be raised by the stub due to network problems.

hi steve - i agree. i'm not using JDK1.4 yet so have created an abstract ChainedException class, that facilitates exception chaining; and have modifed DatabaseException so that it extends ChainedException, so the statcktrace associated with the IOException is preserved.
sai - i guess you are using JDK 1.4 to be able to wrap any DatabaseException within a RemoteException.
actually - do you think I can be penalised for not using 1.4, now it is widely available, especially as the Exception Chaning comes for free in that release ?
also - Steve - are you the Steve Sloggett from Plymouth Uni - Computing and Informatics ?
Steven Sloggett
Greenhorn

Joined: Feb 11, 2002
Posts: 15
Originally posted by dean tomlinson:
actually - do you think I can be penalised for not using 1.4, now it is widely available, especially as the Exception Chaning comes for free in that release ?

As the requirements state any Java 2 version is acceptable, it won't be a problem using any JDK version. Personally, I'm sticking to 1.3, although the paranoid part of me makes me want to stick to JDK 1.2.2 which I'm most familiar with.
I wonder, though, if we should change the exceptions. My reason is that the requirements don't ask for any code cleanup (apart from deprecated methods), and if you were to decide to clean the code, then there's unused variables to remove, misleading comments to correct and much more... I'm not sure that the marker would expect us to go down that path?
also - Steve - are you the Steve Sloggett from Plymouth Uni - Computing and Informatics ?

Oh, and yes, that's me. Small world, huh?
dean tomlinson
Ranch Hand

Joined: Jan 31, 2002
Posts: 94
'kin ell - it is a small world.
did you go back to ibm after uni ? still seeing emma hardy ? where do you live ? - i'm down in bournemouth, working on vodafone's e-commerce site.
how far have you got with this (you sound like you're at a similiar stage to me). i've booked in for my exam on wednesday morning so i'm gonna be working day and night for the next few days - looking forward to nailing this and getting outisde at weekends. planning to go off to australia at the end of summer.
I wonder, though, if we should change the exceptions. My reason is that the requirements don't ask for any code cleanup (apart from deprecated methods), and if you were to decide to clean the code, then there's unused variables to remove, misleading comments to correct and much more... I'm not sure that the marker would expect us to go down that path?

good point - i think there is potentially loads of work that could be done fixing up Data. I noticed getRecordCount returns the total number of records including the deleted ones.
i was advised to leave this side of things alone, and just assume (becasue there is no mention of it in the requirements) that there will not be any deleted records in the fly by night system. what do you reacon ?
really with regard to fixing the exception's in Data, I was just going to modify the constructor, lock and unlock signatures so they throw DatabaseException.
it seems (from what i've read from others) as long as you can defend your decision to do something you are ok .
cheers - dean
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
I used JSDK1.4 so I could wrap the DatabaseException inside RemoteException. In this assignment, it doesn't matter to the user whether you show him the RemoteException or DatabaseException. Whenever there is an exception, I showed a dialog explaining the problem in a simple language along with the stack trace in a text area.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Here is what I did. If the exception that occurs is something that the user needs to know about, and the thrown exception is not a DatabaseException, I create a new DatabaseException with a nice string to let the user know what happened and throw that up to the client.
No if the exception occurs where there might no be a client in existence or probably not in existence like when the server is first starting then I let the server display a message to the server administrator. In this case I don't change the exception type, but might change the message to be nicer.
This way I didn't have to create any new types of exceptions, just use what Java gave us, and what Sun gave us with the assignment.
Mark
Mark
Steven Sloggett
Greenhorn

Joined: Feb 11, 2002
Posts: 15
Originally posted by dean tomlinson:
'kin ell - it is a small world.
did you go back to ibm after uni ? still seeing emma hardy ? where do you live ? - i'm down in bournemouth, working on vodafone's e-commerce site.

No, no, and Basingstoke! I'm working for a company in outer London now.
how far have you got with this (you sound like you're at a similiar stage to me). i've booked in for my exam on wednesday morning so i'm gonna be working day and night for the next few days - looking forward to nailing this and getting outisde at weekends. planning to go off to australia at the end of summer.

Hmm.. well, I've been working on this on and off for a number of months, and have got plenty of prototyped solutions for each part - but haven't yet sorted out all the issues I've come across!
good point - i think there is potentially loads of work that could be done fixing up Data. I noticed getRecordCount returns the total number of records including the deleted ones.
i was advised to leave this side of things alone, and just assume (becasue there is no mention of it in the requirements) that there will not be any deleted records in the fly by night system. what do you reacon ?
really with regard to fixing the exception's in Data, I was just going to modify the constructor, lock and unlock signatures so they throw DatabaseException.
it seems (from what i've read from others) as long as you can defend your decision to do something you are ok .

Yes, at the moment (and subject to more thought on the subject), I think I'm not going to change the exceptions thrown by the supplied code, or tidying it up any more than sorting out the deprecated methods. That said, I'd make sure my code only throws DatabaseExeception from the 'Data' class.
However, perhaps IOException is worth removing. Something for me to ponder I think.
[ June 02, 2002: Message edited by: Steven Sloggett ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

I highly highly recommend NOT REMOVING anything that SUN gave you in your code, except for the deprecated methods. They have very good reasons for the exceptions that they throw. Each and everyone of them makes complete sense.
You might get away with them not noticing that you changed the Exceptions, but if they catch you I think they will mark down your score.
You can put in the design.txt document why you changed it and try to give your best argument, however, I feel as that won't be strong enough.
Please for me, don't change those.
Mark
dean tomlinson
Ranch Hand

Joined: Jan 31, 2002
Posts: 94
hi mark,
you've got me puzzled me now. but you obviously have more than just a feeling about this one.
with regard to not changing the exceptions, is it becasue we do not know that the Data class has not been used in other projects, or already has subclasses which rely on these method signatures ?
I highly highly recommend NOT REMOVING anything that SUN gave you in your code, except for the deprecated methods. They have very good reasons for the exceptions that they throw. Each and everyone of them makes complete sense.

i've had another look in data and i am still confused. it just all looks a bit inconsistent to me.
most methods catch the IOException and convert it into a DatabaseException, which is consistent with my understanding that a method should try to declare exceptions of a kind that are at the same level of abstraction of the method call.
if anything the Data constructor's should probably throw the FileNotFoundException which is a subclass of IOException if the database file cant be found.
also the the comment for the close method says that any attempt to access the database after this has been called will result in an IOexception...
but after close is called (which closes the file then sets db to null), and we then call modify or find . the first line in modify is a call to invariant

/**
* Ensures that the database structure is valid.
* @exception RuntimeException If structure has become corrupted.
*/
protected final synchronized void invariant() {
boolean ok = false;
try {
if (db.length() == headerLen + (recordLen * recordCount)) {
ok = true;
}
} catch (Exception e) {
throw new RuntimeException(UNEXPECTED + e);
}
if (!ok) {
throw new RuntimeException("Data: Internal error");

this will throw a RuntimeException not an IOException.
basically i've took your fint not to change the exception's but i still can't see why i shouldn't. does this go back to do not do anything that is not explicitely asked for ?
if it does, i have designed my dataserver, in a way that enables it to serve multiple databases - if you were me, would you take that out too ?
cheers - dean
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

A RuntimeException does not need to be caught. And in invariant if it is ever thrown it is a major problem, and will cause the app to quit, which is what you want.
As far as the other IOExceptions thrown throughout Data, it is the correct and best exception to throw. When DatabaseException is thrown instead, it is because that is what should be there.
The constructor, readRecord, writeRecord, and seek methods are all dealing with the file which is IO. The other methods are database related and not related to ID, that is why it is wrapped into a DatabaseException.
And also yes to all your questions.
Mark
dean tomlinson
Ranch Hand

Joined: Jan 31, 2002
Posts: 94
thanks a lot mark - i really appreciate your help.
it seems that less is more with this assignment. it's really hard to get used to this - with uni assignments the extra marks come from doing that little bit more.
anyway i was supposed to be uploading my assignment tomorow and sitting the exam on wednesday ! best crack on - gonna do the exam on friday instead.
cheers - dean
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Good Luck Dean. I am sure you will do very well.
Your questions have been very insightful.
Mark
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: IOException