Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Re: DBI error and $@

by notsoevil (Pilgrim)
on Dec 06, 2001 at 21:36 UTC ( #130007=note: print w/replies, xml ) Need Help??

in reply to Re: DBI error and $@
in thread DBI error and $@

This related to Transactions (or this is my intent). According to the 'Perl DBI' book (section 6.3.5 Combining Automatic Error Handling with Transactions):
Imagine combining the automatic error detection of the DBI's RaiseError attribute and the error trapping of Perl's eval { ... } and the error handling properties of transactions. The result is a simple yet powerful way to write robust applications in Perl.
Using RaiseError helps here because it generates a message (or Perl $@ variable value) that includes the underlying error from the driver, and the driver and method names.
If $@ is not containing the error message, why does the example from the book (and Randal's article) use this method of trapping errors?

Jeremiah 49:32 - And their camels shall be a booty. . .

Replies are listed 'Best First'.
Re: Re: Re: DBI error and $@
by Anonymous Monk on Dec 06, 2001 at 22:00 UTC

    I believe you have misunderstood the advice: you combine the approaches, you don't replace one with the other.

    You have to set $@ yourself by calling die in the normal way:

    eval { $db->do($sql) or die $db->errstr(); $db->commit(); } if ($@) { warn "DB error: $@\n"; }
      I believe this is what the RaiseError argument is designed to do: check to be sure each DBI call succeeds, or die.
(arturo) Re^3: DBI error and $@
by arturo (Vicar) on Dec 06, 2001 at 22:05 UTC

    In addition to what the anonymous monk has to say (I'll admit it, it was me, not logged in), take a look at the code that creates the DB handle in that example, where RaiseError is set to 1 (i.e. true). That means the die is automatically called on a DB error, so it's functionally equivalent to the code "AM" provided.


    I mistrust all systematizers and avoid them. The will to a system shows a lack of integrity -- F. Nietzsche

      I have RaiseError set to 1 so like you say, die is called automatically, which should set $@. Is that incorrect? If so, why do the examples not use a explicit die statement with the errstr method -- they, from what I can tell, are just using $@ .. the errstr method is not used at all.

      Jeremiah 49:32 - And their camels shall be a booty. . .

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://130007]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2023-12-08 16:54 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (36 votes). Check out past polls.