exception - Should I throw PHP 7's Error classes in a library? -


i have open-sourced math library php, defines own hierarchy of exceptions:

now php 7 has landed, realize exceptions feel bit redundant new error classes introduced:

let's forget php 5 moment , assume library targets php 7.

  • should throw arithmeticerror , drop arithmeticexception?
  • similarly, should throw divisionbyzeroerror , drop divisionbyzeroexception?
  • should roundingnecessaryexception , numberformatexception extend arithmeticerror?
  • in case, should called roundingnecessaryerror , numberformaterror?

at first glance, seems weird redeclare exceptions have native equivalent in language.

at same time, , although nothing prevents userland code throwing error, feel these classes designed thrown php itself, , userland libraries better off throwing exception , not error.

is there consensus on subject?

you're going have stack overflow moderatory literalists voting close because answers going "primarily opinion based". reason thinks bad or different other answers in general opinion based given questions seldom have single answer.

this reasonable question.

if limit scope of question divisionbyzeroerror , arithmetic error, think should follow php's standardisation here, , leverage errors. moves closer inline principle of least astonishment in makes code in-step expectations 1 have one's own code 1 - example - divides zero. thing (i think 1 can objectively that).

however - subjective - not actively throwing errors, i'd letting code error. ie: don't trap situation , re-throw same error, let code error. simplifies code (objective), , has same end result (objective). ties instinct 1 oughtn't throw these built-in errors. because it's not necessary: php deal you. not intrinsically 1 should not actively throw these errors, s'pose situation in it's legitimate (or necessary) unorthodox one, might beg question if it's right error throw.

as non-specifically-catered situations roundingnecessaryexception vs roundingnecessaryerror... think makes sense upgrade them errors. cannot find reference in docs (someone else might able to, , update this?), state errors programming errors dev need change code fix; , exception exceptional runtime behaviour. or words effect. if 1 wants make method might throw roundingnecessaryerror work, 1 needs necessary rounding. code-time consideration, error makes sense. think subjective there's no cut , dried single answer this, think rationale objectively sound one.


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -