Forums Register Login

Invalid Magic Cookie

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Roberto,

Nice addition

Kind regards,
Roel
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Howdy, Vijay!

Is that what you did?



You bet!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thank you Bob and Jari.
Vijay
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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)
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
Something about .... going for a swim. With this tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1890 times.
Similar Threads
Validation Message and JVM
Validating the Data File
Hard Code Magic Cookie?
Valid Data Files
non-networked and network mode
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 01:06:32.