What's broken about exceptions in Perl?

前端 未结 8 1171
孤城傲影
孤城傲影 2020-11-30 05:43

A discussion in another question got me wondering: what do other programming languages\' exception systems have that Perl\'s lacks?

Perl\'s built-in exceptions are a

8条回答
  •  悲哀的现实
    2020-11-30 06:20

    A problem I recently encountered with the eval exception mechanism has to do with the $SIG{__DIE__} handler. I had -- wrongly -- assumed that this handler only gets called when the Perl interpreter is exited through die() and wanted to use this handler for logging fatal events. It then turned out that I was logging exceptions in library code as fatal errors which clearly was wrong.

    The solution was to check for the state of the $^S or $EXCEPTIONS_BEING_CAUGHT variable:

    use English;
    $SIG{__DIE__} = sub {
        if (!$EXCEPTION_BEING_CAUGHT) {
            # fatal logging code here
        }
    };
    

    The problem I see here is that the __DIE__ handler is used in two similar but different situations. That $^S variable very much looks like a late add-on to me. I don't know if this is really the case, though.

提交回复
热议问题