Is it better to croak() or to die() when something bad happens in Perl?

后端 未结 6 1180
无人及你
无人及你 2021-02-18 22:12

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

# Connect to database
my $db_ha         


        
6条回答
  •  鱼传尺愫
    2021-02-18 22:34

    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.

提交回复
热议问题