What's broken about exceptions in Perl?

前端 未结 8 1163
孤城傲影
孤城傲影 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:18

    With Perl, language and user-written exceptions are combined: both set $@. In other languages language exceptions are separate from user-written exceptions and create a completely separate flow.

    You can catch the base of user written exceptions.

    If there is My::Exception::one and My::Exception::two

    if ($@ and $@->isa('My::Exception'))
    

    will catch both.

    Remember to catch any non-user exceptions with an else.

    elsif ($@)
        {
        print "Other Error $@\n";
        exit;
        }
    

    It's also nice to wrap the exception in a sub call the sub to throw it.

提交回复
热议问题