当 Perl 中发生不好的事情时,是 croak() 还是 die() 更好?

发布于 2024-10-02 07:15:24 字数 720 浏览 8 评论 0原文

perlcritic 抱怨以下代码,一些样板 DBI 内容工作得很好,应该 croak 而不是 die

# Connect to database
my $db_handle = DBI->connect( $url, $user, $password ) or die $DBI::errstr;

所有这些,而 die 似乎对我来说工作得很好。

我认为对于一个武士 Perl 战士来说,当事情出错时,呱呱叫比实际死亡更不光荣。笑话分开

为什么我应该呱呱而不是

不听取 perlcritic 建议的后果是什么?

perlcritic complaints that the following code, some boilerplate DBI stuff that works perfectly fine, should croak instead of die:

# Connect to database
my $db_handle = DBI->connect( $url, $user, $password ) or die $DBI::errstr;

All this, while die seems to work fine for me.

I would think for a samurai Perl warrior, croak is less honorable than actually die when things go awry. Jokes apart

Why should I croak instead of die?

What are the consequences of not heeding perlcritic's advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我乃一代侩神 2024-10-09 07:15:24

来自 http://www.perlmonks.org/?node_id=685452

当错误是您或您的代码没有正确执行时,您可以使用 die。当你的呼叫者做得不对时,你可以使用 croak。死亡“错误:$!”表示错误发生在发生错误的行上。呱呱叫“错误:$!”指示错误发生在调用者调用您的代码的行上。

在这种情况下,错误(与数据库的连接错误)与调用者无关,而与建立连接的线路有关,因此我将使用die。

From http://www.perlmonks.org/?node_id=685452

You use die when the error is something you or your code didn't do right. You use croak when it's something your caller isn't doing right. die "error: $!" indicates the error is on the line where the error occured. croak "error: $!" indicates the error is on the line where the caller called your code.

In this case, the error (connection error to DB) has nothing to do with the caller and everything to do with the line making the connection, so I would use die.

蓝海 2024-10-09 07:15:24

我通常使用以下内容:

  • die "string" 来表示您想要直接传达给用户的致命消息。我主要在脚本中执行此操作。
  • 对于完整的异常对象,die $object,尽管大多数异常类都会有 throw 方法为您执行此操作。这是为了当你的调用者应该能够知道你抛出了什么样的错误,甚至可以从中提取信息。如果您使用的是 Moose,请查看 ThrowableThrowable-X< /a>
  • croak "message" 就像 Adrian 所说的那样,当你的用户做了错事,并且你想在任何调用你的代码的地方报告错误时。函数和 API 级方法是这种情况的常见情况。
  • 对于库错误,我还没有看到异常的用处,承认“消息”。这些通常是您认为是错误的运行时错误,而不是异常情况。对这些使用异常是有意义的,特别是如果您有一个已经使用异常的大型项目。但这是获取错误堆栈跟踪的好方法。

I usually use the following:

  • die "string" for fatal messages you want to directly communicate to the user. I mostly do this in scripts.
  • die $object for full blown exception objects, although most exception classes will have throw method doing that for you. This is for when your caller should be able to tell what kind of error you throw, and maybe even extract information out of it. If you're using Moose, check out Throwable and Throwable-X
  • croak "message" like Adrian said is for when your user has done something wrong, and you want to report the error at whatever called your code. Functions and API level methods are usual cases for this.
  • confess "message" for library errors where I don't see the usefulness of an exception yet. These are usually runtime errors you assume to be a mistake, rather than an exceptional condition. It can make sense to use exceptions for these, especially if you have a large project that uses exceptions already. But it's a good, nice and easy way to get a stacktrace with the error.
眼波传意 2024-10-09 07:15:24

如果我们在函数外部调用 die 和 croak,则没有
这些功能之间的区别。

只有当我们在任何情况下调用 die 和 croak 时,我们才能发现差异。
其他功能。

croak 将提供有关调用者的信息,例如函数名称
和行号..等。
die 会给出错误并给出发生错误的行号。

If we called the die and croak outside the function there is no
difference between these functions.

We can only find difference when we call die and croak within any
other function.

croak will gives information about the caller such as function name
and line number..etc.
die will give error and gives the line number where the error has occured.

零度° 2024-10-09 07:15:24

使用 die() 不一定是错误的,但 croak() 可以为您或您的用户提供更多有关问题所在的信息。还可以在 Carp 命名空间中设置一些变量,这些变量可以更改此输出以获取更多或更少的信息。

它相当于 die() 但具有更多信息和控制。

It's not necessarily wrong to use die() but croak() gives you or your user a lot more information about what went wrong. There's also variables that can be set in the Carp namespace that can change this output to get more or less information.

It's equivalent to die() but with more information and control.

放肆 2024-10-09 07:15:24

仍然。如果连接参数如其他人指出的那样直接来自函数的输入,则此示例可能需要使用 croak。

如果您为自己编写一个触发异常的测试,那么您将成为自己的客户并欣赏哪个更好。

如果测试错误条件非常困难,则可能会出现死机的情况。但任何参数验证错误都应该使用 croak。

Still. This example may want to use croak if the connection parameters came directly from the inputs to the function as others pointed out.

If you write a test for yourself that triggers the exception then you will be your own client and appreciate which is better.

If it is very hard to test error condition it may be a case for die over croak. But any parameter validating errors should use croak.

梦幻之岛 2024-10-09 07:15:24

Carp 例程在您自己的模块中很有用,因为它们的行为类似于 die() 或 warn(),但带有一条更有可能对您的模块的用户有用的消息。< /p>

https://perldoc.perl.org/Carp

The Carp routines are useful in your own modules because they act like die() or warn(), but with a message which is more likely to be useful to a user of your module.

https://perldoc.perl.org/Carp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文