Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jr0y9-0005os-1F for pgsql-general@arkaria.postgresql.org; Thu, 02 Jul 2020 15:17:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jr0y8-0004YZ-0V for pgsql-general@arkaria.postgresql.org; Thu, 02 Jul 2020 15:17:44 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jr0y7-0004Y1-OT for pgsql-general@lists.postgresql.org; Thu, 02 Jul 2020 15:17:43 +0000 Received: from mail.thenautilus.net ([5.79.87.27]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jr0y5-0007pO-FX for pgsql-general@lists.postgresql.org; Thu, 02 Jul 2020 15:17:42 +0000 Received: (qmail 30097 invoked from network); 2 Jul 2020 15:17:23 -0000 Received: from unknown (HELO exelion) (dakkar@::ffff:10.111.0.30) by 0 with ESMTPA; 2 Jul 2020 15:17:23 -0000 Date: Thu, 2 Jul 2020 16:17:22 +0100 From: Gianni Ceccarelli To: pgsql-general@lists.postgresql.org Subject: Re: Catching errors with Perl DBI Message-ID: <20200702161722.340d4fce@exelion> In-Reply-To: <20200702150337.GA18581@panix.com> References: <20200702150337.GA18581@panix.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, 2 Jul 2020 11:03:37 -0400 stan wrote: > my $sth =3D $dbh->prepare($stmt); > my $rv =3D $sth->execute() or die $DBI::errstr; that ``or die`` means: if the result of the ``execute`` is false (which only happens on error), throw an exception (which, as you noticed, terminates the process unless caught with an ``eval {}`` or similar construct) > if ( $rv < 0 ) { print $DBI::errstr; } Notice that ``$rv`` would never be less than 0: for an ``INSERT``, it's the number of rows inserted (or a special "0 but true" value in case no rows were inserted). So, you can do two things: * keeping the same style:: my $rv =3D $sth->execute(@bind_values); if (!$rv) { print $sth->errstr; # and probably do something useful here =E2=98=BA } * switching to exceptions everywhere Tell DBI you want exceptions:: my $dbh =3D DBI->connect( $dsn,$user,$password, { PrintError =3D> 0, RaiseErorr =3D> 1, PrintWarn =3D> 0, RaiseWarn =3D> 1, } ); then run statements like this:: eval { $dbh->prepare($stmt)->execute(@bind_values) } or do { print $@; # the exception is store in this variable }; --=20 Dakkar - GPG public key fingerprint =3D A071 E618 DD2C 5901 9574 6FE2 40EA 9883 7519 3F88 key id =3D 0x75193F88