Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you name and organize your exceptions?

When introducing new exception types I am always a but unsure how to do this correctly. Is there a common convention? How do you do it?

I am interested in the scope you organize them (Keep them in the unit they are used in? Have a unit on component level? Package level? Application?)

This also influences naming. How much context do you include? Is it better to make them very specific (like EPersonIDNotFoundError) or try to make them reusable (like ENotFoundError)?

And what about the suffix "Error" - when should I add it and when leave it? I cannot see the logic e.g. in Classes.pas:

  EWriteError = class(EFilerError);
  EClassNotFound = class(EFilerError);
  EResNotFound = class(Exception);
like image 356
Heinrich Ulbricht Avatar asked Nov 11 '11 10:11

Heinrich Ulbricht


1 Answers

The only real convention I know of, is to prefix them with E. I haven't really given it much thought in the past, but now I think of it, it seems to me that both Error and Exception are commonly used as a postfix. If you mix them, I'd say that Exception relates to something that goes wrong unexpectedly, like a connection that is broken, or a file that turns out to be unreadable, while an error relates more to wrong input, for instance a number was expected, but someone typed text.

The VCL seems to follow certain conventions too, but it seems to add a postfix only if it wouldn't be clearly and error without it, for instance

EConvertError, EMathError, EVariantError

vs

EAccessViolation, EInvalidPointer, EZeroDivide

The latter describe the error itself, where the first list need the postfix to indicate an error in a certain process or entity.

These examples are found in SysUtils, maybe you can take a look there, because it contains many exception classes as well as base classes for an even larger amount of exception. Very few of those end in Exception, except for some very specific errors that indeed you hope to never come across, like EHeapException and ESafecallException.

like image 127
GolezTrol Avatar answered Sep 27 '22 19:09

GolezTrol