Archived

This topic is now archived and is closed to further replies.

[java] Exception vs Error Checking

This topic is 5501 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites