Opinions on Exceptions

I am doing some design work and was wondering what everyone else thought of using exceptions in a game. I am thinking of using them say, for example: if a player tried to equip an item that was not flagged to be equipped it would generate an exception [ItemCannotBeEquippedException]. This seems like an improper use for try and catch statements and exceptions. As an exception is defined as something that hinders the flow of the problem. Though, this hinders the flow, it does not necessary impact it on a way that is a very serve problem (such as an out of bounds exception on an array list). I am used to doing checking myself in if statements. ie: if item not flagged return “Can’t equip this.”

What are your thoughts on this?

Another question I had - what is the performance hit on using try/catch in java? Does anyone have any good articles on that?

Thank you.

If the game allows the player to attempt to equip the item, then it should not be an exception. Exceptions should not be used for things you expect to occur in your code. Exceptions should cater for things that are outside your control (IO errors, missing data files etc) or which shouldn’t happen (array overruns, missing parameters etc) not for things which are quite expected in the program.

My approach is to code for what I expect the program ought to be dealing with and leave any other possibilities that I’m aware of be handled by exceptions.

Efficiency of exceptions is quite good in Java, I believe. A lot better than in c++.

If something can naturally occurs in your game (i.e. the example you described) then you should not be throwing exceptions. Exceptions are for exceptional situations, as their name implies.

What you described is using exceptions for control flow, which is a bad idea. The exception code adds a (small) runtime overhead, but it also drastically increases the verbosity of the code you write; which is never a good thing.

The final point is that it decreases the implied ‘meaning’ of the error. The more exceptions you have the more you increase the chance of a critical exception being overlooked during debugging and standard runtime. Throwing exception also leads to lazy habits, just doing a catch(Exception e) for example… it’s not a good thing to do.

Thanks a lot for the help guys.

PS: Been busy moving and working.