What's broken about exceptions in Perl?

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

    It has been a looong time since I used Perl, so my memory may be fuzzy and/or Perl may have improved, but from what I recall (in comparison with Python, which I use on a daily basis):

    1. since exceptions are a late addition, they are not consistently supported in the core libraries

      (Not true; they are not consistently supported in core libraries because the programmers that wrote those libraries don't like exceptions.)

    2. there is no predefined hierarchy of exceptions - you can't catch a related group of exceptions by catching the base class

    3. there is no equivalent of try:... finally:... to define code that will be called regardless of whether an exception was raised or not, e.g. to free up resources.

      (finally in Perl is largely unnecessary -- objects' destructors run immediately after scope exit; not whenever there happens to be memory pressure. So you can actually deallocate any non-memory resources in your destructor, and it will work sanely.)

    4. (as far as I can tell) you can only throw strings - you can't throw objects that have additional information

      (Completely false. die $object works just as well as die $string.)

    5. you cant get a stack trace showing you where the exception was thrown - in python you get detailed information including the source code for each line in the call stack

      (False. perl -MCarp::Always and enjoy.)

    6. it is a butt-ugly kludge.

      (Subjective. It's implemented the same way in Perl as it is everywhere else. It just uses differently-named keywords.)

提交回复
热议问题