Search code examples
exceptionperspective

Naming exceptions in code


I am writing a compiler. And as you know there can be tons of different errors one may need to handle. For e.g. if you are inserting a value in the symbol table that already exists, you would throw an exception (and then catch it in the code somewhere).

I wanted to get some perspective from stack overflow as to how people name their own custom exceptions.

  • Do you follow any special rules?
  • Are they simply some names that come to your mind straightway?
  • How long are the names? For e.g. SymbolValueInTableException sounds too much!

Thanks for sharing your perspective.

(I can show some code if you want but it's pretty pointless to the discussion)


Solution

  • The exception name should clearly outline what kind of error it is your program has encountered. For example, "NullPointerReferenceException" explicitly outlines the exact error you have encountered - a null pointer reference! Longer is generally better so long as it is accurate. In the end, it's up to you. Comment well and all will be well.