I generally wrap all DBI-related procedures in eval blocks. This includes statement prepares & executes. Here's an example for DBI connections:
my $dbh; eval { $dbh = DBI->connect($dsn, $stuff, { RaiseError => 1 }); # will die if it doesn't work } if ($@) { # you can process the error string in $@ and clean it up # for output print $error; }
Of course, if you connect to the DB with RaiseError set, you will have to wrap all DB-related methods in eval blocks to avoid die'ing.

Also, your code has if (undef $dbh) to check for connection errors. Yikes! This will set the value of $dbh to undef each time, and will always return undef, so the top part of your if-statement will never be executed, only the else-block. This may be why you're having problems. You probably meant to say if (not defined $dbh).

blokhead


In reply to Re: Graceful handling of DBI connect errors' by blokhead
in thread Graceful handling of DBI connect errors' by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.