• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaring RuntimeExceptions

 
Yupp Cook
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Developer in Spe!

Just a short question:
Is it advisable to declare own Exceptions derived from RuntimeException in the method declaration?
Since we all passed the Programmer Exam (hopefully) we know that RuntimeExceptions don't need to be declared.
I throw my own RuntimeException called DbIOException after catching an IOException.

Thanx
Yupp
 
Kevin Conaway
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yupp,

My personal opinion is that you should not throw RuntimeExceptions in place of checked exceptions.

See this thread for my thoughts on the subject.

Kevin
[ February 10, 2006: Message edited by: Kevin Conaway ]
 
Yupp Cook
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kevin

Thats exactly what I think. The original IOException shouldn't be changed into an unchecked. But there are two cases where I think I don't have a choice (BS_2.1.2):

public int[] find(String[] criteria); // no Excpt !
public int create(String[] data) throws DuplicateKeyException;

How can I handle the possible IOExceptions coming from DBFileManager without RuntimeExceptions since it's not allowed to throw any in find and just the akward DuplicateKeyExceptions in create

By the way what the sense of DuplicateKeyException? I just don't throw it.
 
Kevin Conaway
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yupp,

Regarding find() not throwing any exceptions, I think the subtle hint that Sun is giving you is that you should not be doing any kind of file i/o during find(). Maybe keeping a logical copy of the database in memory?

As far as DuplicateKeyException, I would handle it the same way as RecordNotFoundException. Subclassing + Exception chaining.

Kevin
 
Yupp Cook
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kevin.

You might be right. I ll take this into consideration.
Are you faced with the same exceptionless declarations in your assignment?

What can cause the DuplicateKeyException while creating a new record?
My method searches the records for an invalid one. If found it replaces the rec with the new one otherwise its added at the end after the last record increasing the total number of records by one. The method is totally synched.

Regards
Yupp
 
Kevin Conaway
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yupp,

I took DuplicateKeyException to really mean "DuplicateRecordException" such that if you try to insert a new Record and a Record with the exact same fields already exists, then an Exception is thrown

Kevin
 
Yupp Cook
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kevin

Would be a way to put it. But what if the client wants to create an exact copy of a record? My spec doesn't forbid that.

Yupp
 
Eiji Seki
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What can cause the DuplicateKeyException while creating a new record?


Since my specification does not mention any key for the values, I assume there is none. Except of course our internal record number, but since it is managed by us, the exception will never ocurr.

Note that these are based on the fact that my data store mechanism does not understand at all the "meaning" of the values. Since all Strings are just text values, not names, locations, etc, it is not possible to point a key.

Also, as Yupp stated, look for "no duplicated records" restrictions, but there are none on my spec.

Maybe the exception is there because of other versions of the spec (note that method signatures are different), maybe the intention is confuse us.
 
Kevin Conaway
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My spec does not forbid it either. Then again, it does not say anything about it. Its just my interpretation of it, one that I will explain in my choices.txt
 
Yupp Cook
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So under which circumstances this DuplicatKeyException should be thrown?
Or shouldn't it be thrown at all..? I actually started liking the idea of unique records.

Yupp
 
Eiji Seki
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been thinking of reasons for inserting two identical records on a data storage and found none. All reasonable situations would gladly accept an extra piece of information that would make the records different (for example, product serial number or room number).

BUT looking at my spec, I just discovered that my db file accepts identical records. I am building the URLyBird, a room booking system, but to my surprise the data stored do not have any specific information about the room itself. So if a hotel has more than one room with the same characteristics (price, beds, smoking, for example) then many identical records may appear on the database. Notice that it is almost sure that a hotel has similiar rooms.

Yes, should I have designed the data file myself, I would have included some key information like room number, but we must not discuss the spec.

Be careful and just make sure you don't have a similar situation or else DuplicateKeyException in case of identical records may be a problem.
 
Felipe Regalgo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eiji

The assignment says

The company's IT department has a data file that contains the essential information for the company, but because the data must continue to be manipulated for reports using another custom-written application, the new system must reimplement the database code from scratch without altering the data file format

I would have included some key information like room number


I think that database should be equals the especifications so if you create a new key information you will not need to use DuplicateKeyException its good, but I think you are out especification...

So I think before create a new record you have to use the find method to make sure that won't create a DuplicateRecord....

what do you think about??
 
Eiji Seki
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Felipe,
I never intented to modify the data file or it's schema. I just made a comment that IF I was the one responsible for designing the data, I would have a primary key.
 
Sergey Zolotaryov
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with those of you, who chose not to throw this exception at all. The customer (specification writer), did not put any unique constraints on the data, so I should not do what I am not asked for. The assignment checks not only programming skills, but the ability to understand and STICK to the spec. I will explain this decision in the choices.txt.
 
Yupp Cook
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think both ways(unique and duplicate recs) are reasonable.
Duplicate records can be considered senseless cause the database represents just an "is" booking state without any date relation. So what's the point of having the same record twice?

I go for DuplicatKeyException.

Yupp.
[ March 14, 2006: Message edited by: Yupp Cook ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic