Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invalid Magic Cookie

 
Vijay Sai
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've a question regarding Magic Cookie -

Let's say that the magic cookie I read is invalid, Should I display the error message to the user & exit? or throw an exception?

Thank you,
Vijay
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijay,

I think the best option you could do, is throw an exception, catch it and let the user select another database file (that's how i did it). You can of course also exit the application but i think that's maybe not that user friendly

Kind regards,
Roel
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Vijay! Welcome to JavaRanch!

The specs are not very clear about the magic cookie. For instance, it says that it identifies a data file as being valid, but it doesn't say what is the correct value. I myself assumed that the value that appears in the .db file that was sent to me was the right one. So, when I load the database records to the memory cache I have, I compare the value found in the provided .db file with the value of the .db file that was sent to me; if they don't match, then I throw an InvalidDatabaseException (which I created).

But I also saw some people that just ignored this value and assumed that the given .db file is valid, and passed without any problem.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roberto,

Nice addition

Kind regards,
Roel
 
Vijay Sai
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Roel and Bob.

I do think throwing an exception is better than exitting the app. I'm planning to create a checked exception. Is that what you did?

Thanks once again,
Vijay
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Vijay!

Is that what you did?


You bet!
 
Jari Timonen
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing to remember regarding exceptions: catch/log OR throw forward. Never do both, since you might end up printing same exception multiple times and it just confuses.
 
Vijay Sai
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Bob and Jari.
Vijay
 
Robert Benson
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've got a draft version working but have not catered for the magic cookie test.

There is an argument as to whether to code or not. I think its better to code but it depends on how much rework I need to do. I may reconsider my solution and leave out . My Data method currently throws an IOException. If I use this, there will be little rework and I will use for the magic cookie check.

But I am not sure if the "magic cookie error check" could be catergorized as an IOException.

Could I use IOException or should a different exception be used?

What do you think?

Robert.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a magic cookie error is not an IOException, so I would opt for another exception. I used a DBException, which is also used to wrap IOExceptions. Because your interface should not throw IOExceptions, because an IOException doesn't hide implementation details (I know you use a file). And when you switch to a RDBMS where IOException doesn't exist (but you'll have a SQLException) you will have a lot of work changing your code. You should always try to code as much as possible against interfaces (which are kept generic and hide implementation details), so you can easily change the current implementation (which uses a file), with a new implementation (which uses a RDBMS for example)
 
Robert Benson
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Roel,
I think I will leave out the check as it will require rework and I dont want to introduce an error. While it is nice to have, it is not a requirement. I am coding for a solution that will pass without bells and whistles.

Robert.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Robert!

One thing you can do is include in your choices.txt file that you didn't include this magic cookie checking because it is not explicit in the assignment whether you have to include it in your solution or not. I think it is important to mention this or other reason you may think of in the choices.txt file.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic