exception handling in plperlu

Started by Jasbinder Baliabout 19 years ago11 messagesgeneral
Jump to latest
#1Jasbinder Bali
jsbali@gmail.com

Hi,
Actually, if I rephrase my requirement, I need to catch an exception at any
point ,where ever it is raised, in the perl code.
E.g during an insert, there is a foreign key contraint violation, then i
need to catch this specific error and do something with it.
Hope i make some sense here.

Thanks,
Jas

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Jasbinder Bali (#1)
Re: exception handling in plperlu

"Jasbinder Singh Bali" <jsbali@gmail.com> writes:

Actually, if I rephrase my requirement, I need to catch an exception at any
point ,where ever it is raised, in the perl code.
E.g during an insert, there is a foreign key contraint violation, then i
need to catch this specific error and do something with it.

Since PG 8.0, you should be able to trap exceptions with eval{}, same as
you would do in any Perl code.

regards, tom lane

#3Jasbinder Bali
jsbali@gmail.com
In reply to: Tom Lane (#2)
Re: exception handling in plperlu

How about using a try catch block?
isn't that more efficient that eval?

~Jas

Show quoted text

On 3/15/07, Tom Lane <tgl@sss.pgh.pa.us> wrote:

"Jasbinder Singh Bali" <jsbali@gmail.com> writes:

Actually, if I rephrase my requirement, I need to catch an exception at

any

point ,where ever it is raised, in the perl code.
E.g during an insert, there is a foreign key contraint violation, then i
need to catch this specific error and do something with it.

Since PG 8.0, you should be able to trap exceptions with eval{}, same as
you would do in any Perl code.

regards, tom lane

#4Martijn van Oosterhout
kleptog@svana.org
In reply to: Jasbinder Bali (#3)
Re: exception handling in plperlu

On Thu, Mar 15, 2007 at 10:06:06PM -0400, Jasbinder Singh Bali wrote:

How about using a try catch block?
isn't that more efficient that eval?

Umm, eval is perl's equivalent of try/catch. There is no other way.

Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/

Show quoted text

From each according to his ability. To each according to his ability to litigate.

#5Jasbinder Bali
jsbali@gmail.com
In reply to: Martijn van Oosterhout (#4)
Re: exception handling in plperlu

just wondeng why doesn't it let me put
my $dbh=DBI->connect("dbi:Pg:dbname=dbunmask; host=192.168.0.120;
port=5432;", "", "");
in eval

says
Global symbol "$dbh" requires explicit package name at line <where ever dbh
is used>

Jas

Show quoted text

On 3/16/07, Martijn van Oosterhout <kleptog@svana.org> wrote:

On Thu, Mar 15, 2007 at 10:06:06PM -0400, Jasbinder Singh Bali wrote:

How about using a try catch block?
isn't that more efficient that eval?

Umm, eval is perl's equivalent of try/catch. There is no other way.

Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/

From each according to his ability. To each according to his ability to

litigate.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFF+oqkIB7bNG8LQkwRApyEAJ413cmp0bIuasEceWhXgTgSE6NOFACeNijH
ZHknBQrtHqg30xL8Wh219Ik=
=Xhhm
-----END PGP SIGNATURE-----

#6A.M.
agentm@themactionfaction.com
In reply to: Jasbinder Bali (#5)
Re: exception handling in plperlu

On Mar 16, 2007, at 10:54 , Jasbinder Singh Bali wrote:

just wondeng why doesn't it let me put
my $dbh=DBI->connect("dbi:Pg:dbname=dbunmask; host=192.168.0.120;
port=5432;", "", "");
in eval

says
Global symbol "$dbh" requires explicit package name at line <where
ever dbh is used>

There is a mailing list for DBD::Pg:
http://gborg.postgresql.org/mailman/listinfo/dbdpg-general/

But your problem in not related to the driver, rather it's a Perl
issue. Your variable is defined within the block, so its scope is the
eval block ("my" implies lexical scoping). If you define it outside
the block and assign inside the block, then it will work.

Cheers,
M

#7Adam Rich
adam.r@sbcglobal.net
In reply to: Jasbinder Bali (#5)
Re: exception handling in plperlu

You'll want something like this:

my $dbh = eval { DBI->connect(....);};

-----Original Message-----
From: pgsql-general-owner@postgresql.org
[mailto:pgsql-general-owner@postgresql.org] On Behalf Of Jasbinder Singh
Bali
Sent: Friday, March 16, 2007 9:55 AM
To: Martijn van Oosterhout; Tom Lane; pgsql-general@postgresql.org
Subject: Re: [GENERAL] exception handling in plperlu

just wondeng why doesn't it let me put
my $dbh=DBI->connect("dbi:Pg:dbname=dbunmask; host=192.168.0.120;
port=5432;", "", "");
in eval

says
Global symbol "$dbh" requires explicit package name at line <where ever
dbh is used>

Jas

On 3/16/07, Martijn van Oosterhout < kleptog@svana.org> wrote:

On Thu, Mar 15, 2007 at 10:06:06PM -0400, Jasbinder Singh Bali wrote:

How about using a try catch block?
isn't that more efficient that eval?

Umm, eval is perl's equivalent of try/catch. There is no other way.

Have a nice day,
--
Martijn van Oosterhout < kleptog@svana.org>
http://svana.org/kleptog/

From each according to his ability. To each according to his ability

to litigate.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFF+oqkIB7bNG8LQkwRApyEAJ413cmp0bIuasEceWhXgTgSE6NOFACeNijH
ZHknBQrtHqg30xL8Wh219Ik=
=Xhhm
-----END PGP SIGNATURE-----

#8Martijn van Oosterhout
kleptog@svana.org
In reply to: Jasbinder Bali (#5)
Re: exception handling in plperlu

On Fri, Mar 16, 2007 at 10:54:49AM -0400, Jasbinder Singh Bali wrote:

just wondeng why doesn't it let me put
my $dbh=DBI->connect("dbi:Pg:dbname=dbunmask; host=192.168.0.120;
port=5432;", "", "");
in eval

says
Global symbol "$dbh" requires explicit package name at line <where ever dbh
is used>

Well, eval starts a new scope so the $dbh is only defined within that
block. From the errors I assume you tried the other statements outside
that scope?

Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/

Show quoted text

From each according to his ability. To each according to his ability to litigate.

#9Doug McNaught
doug@mcnaught.org
In reply to: Jasbinder Bali (#5)
Re: exception handling in plperlu

"Jasbinder Singh Bali" <jsbali@gmail.com> writes:

just wondeng why doesn't it let me put
my $dbh=DBI->connect("dbi:Pg:dbname=dbunmask; host=192.168.0.120; port=5432;",
"", "");
in eval

says
Global symbol "$dbh" requires explicit package name at line <where ever dbh is
used>

The my() variable goes out of scope when the eval {} is done. Declare
the variable outside the eval {}:

my $dbh;

eval { $dbh = connect(...) };

eval { $dbh->prepare(...:) };

-Doug

#10Randal L. Schwartz
merlyn@stonehenge.com
In reply to: Doug McNaught (#9)
Re: exception handling in plperlu

"Douglas" == Douglas McNaught <doug@mcnaught.org> writes:

Douglas> my $dbh;

Douglas> eval { $dbh = connect(...) };

Since eval returns its value (or undef if $@ has the error), you can shorten
this to:

my $dbh = eval { DBI->connect(...) };

and now either look at $@ (if you want to distinguish exception vs normal) or
just defined $dbh (if you want to know if you have a useful handle or not).

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/&gt;
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

In reply to: Jasbinder Bali (#5)
Re: exception handling in plperlu

On 3/16/07, Jasbinder Singh Bali <jsbali@gmail.com> wrote:

just wondeng why doesn't it let me put
my $dbh=DBI->connect("dbi:Pg:dbname=dbunmask;
host=192.168.0.120; port=5432;", "", "");
in eval

you dont need to put ->connect in eval.
just set connect option 'raiseerror' to 0 (and printerror as well). in
this situation it will simply set errorcode. no eval{} required.

depesz