in reply to Re^2: Catching a 'division by zero' error with Exception::Class
in thread Catching a 'division by zero' error with Exception::Class

dear massa, thanks for your help. Would you mind to give a quick explanation of the effect of this eval wrapper around the division by zero expression?
  • Comment on Re^3: Catching a 'division by zero' error with Exception::Class

Replies are listed 'Best First'.
Re^4: Catching a 'division by zero' error with Exception::Class
by SuicideJunkie (Vicar) on Sep 15, 2008 at 17:10 UTC

    The important bit is that you are eval-ing the '27/0', but the "|| throw exception" part is outside the eval. So when the eval dies out of the execution and returns false, the exception is thrown.

    The structure is eval {"risky expression"} or throw exception.
    NOT eval {"risky expression; or throw exception"}

    It should be noted that the return value of eval is:

    "...the value of the last expression evaluated inside the mini-program; a return statement may be also used, just as with subroutines."
    So, you should add a "1;" or "return 1;" to the end, otherwise you would throw some spurious exceptions if the answer is supposed to be zero (such as 5*2-10)