[java] Exception vs Error Checking

Started by
8 comments, last by AndyTang 21 years, 4 months ago
I am creating a game in Java at the moment. I guess what I need to know is whether using Exception is better than checking for errors itself. i.e. Is it better to check for an out of bound error (ie less than 0) or it is better to let Exception to check and use catch instead for the result. It seems that Exceptions create a cleaner code but I don''t know if they are better and more effeicent than binary checks.
Advertisement
bonus points for research: http://www.javagaming.org/cgi-bin/JGOForums/YaBB.cgi?board=Tuning;action=display;num=1036197920;start=12
Star Dart - Java Multiplayer Space Combat at http://www.stardart.net/
Thanks for that, unfortunately I need to register to use that forum.
eh?
Star Dart - Java Multiplayer Space Combat at http://www.stardart.net/
quote:Original post by AndyTang
It seems that Exceptions create a cleaner code but I don''t know if they are better and more effeicent than binary checks.

You have your answer right there. Unless you know that a particular piece of code is a bottleneck, you should always favor clean code.


God puts an apple tree in the middle of the Garden of Eden and says, do what you like guys, oh, but don''t eat the apple. Surprise surprise, they eat it and he leaps out from behind a bush shouting "Gotcha." It wouldn''t have made any difference if they hadn''t eaten it... because if you''re dealing with somebody who has the sort of mentality which likes leaving hats on the pavement with bricks under them you know perfectly well they won''t give up. They''ll get you in the end. -- Douglas Adams
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Actually, the answer is in the thread I linked to.
Star Dart - Java Multiplayer Space Combat at http://www.stardart.net/
When deciding whether to use exceptions, ask yourself if that condition is exceptional. This means that the condition must be something out of the ordinary. This is down to you to define what it ordinary.

Some things that are NOT ordinary are an EOF (occurs in every file), end of line, etc.

Some things that IMHO are best handled as exceptions are NULL pointers, out-of-memory situations, and the like.

Hope that helps.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
quote:Original post by markuskidd
Actually, the answer is in the thread I linked to.


Sorry I didnt make myself clearer before. The thread you linked to, I cant access cause I am not a member. I need to register before I can access it apparently.

Thanks for all your answers.
Taking the time to register at the javagaming.org forums is really going to be worth it if you are developing games in Java. It''s quite a resource.
Star Dart - Java Multiplayer Space Combat at http://www.stardart.net/
In a nut shell, exceptions are slower. If you use them for "EXCEPTIONS" (meaning they don''t happen often) then you are golden.


I have exceptions all over my code, and if things work right no exception is ever called, hence no slow down. So I use exceptions for things like "InsertObject" failed. This method should succeed every time. But if I run out of memory, or something odd like that, the function will fail. If I used an error code, every single call I would be checking the error code to see if it succeeded. And 99.999% of the time it will, so I''m really wasting my time checking that error code.

My catch clause logs the error and the system continues on normally without inserting the new object (not a fatal error).
He''s a bad motha - Shut yo mouth.

This topic is closed to new replies.

Advertisement