How can I avoid a Die being raised, the handling used does not help

Discussion in 'Perl Misc' started by daniel.crosby, Nov 17, 2006.

  1. the resulting output occassionaly produces ERROR DIE main:
    Can't call method "prepare" on an undefined value at line 1365. - No
    event sent!
    How can I prevent a die from occurring setting PrintError=>0,
    RaiseError=>0 does not seem to help.

    local $SIG{ALRM} = sub { die "TIMEOUT: unable to connect
    database $G_CONF{INSERT_DB} \n"};

    alarm $G_CONF{TIMEOUT_INVDB};

    $l_cstr="DBI:Oracle:$G_CONF{INSERT_DB}";

    $l_h =
    DBI->connect($l_cstr,$G_CONF{INSERT_USER},$G_CONF{INSERT_PASS}, {
    PrintError=>0, RaiseError=>0, AutoCom
    mit=>1 } );
    &Logging (2, $I, "insert", "prepare $l_insert");
    -> line 1365 is below prepare
    $l_x=$l_h->prepare ($l_insert ) or &Error ("Prepare $l_insert
    failed", "insert");
    $l_x->execute or &Error ("Execute $l_insert failed", "insert");


    $G_DBCON->disconnect or &Warn ( "Can not close config DB
    $G_CONF{CONFIG_DB} - $DBI::errstr", "disconnect");
    alarm 0;
    };
     
    daniel.crosby, Nov 17, 2006
    #1
    1. Advertisements

  2. daniel.crosby

    anno4000 Guest

    You can wrap the offending statement in an eval block, but really
    you shouldn't call prepare() at all when the preceding connect
    doesn't return a defined value.
    Check the result. If $l_h isn't defined, you can't call the prepare()
    method on it. Catching the error won't help, because the actual error
    happened earlier in connect().
    Anno
     
    anno4000, Nov 17, 2006
    #2
    1. Advertisements


  3. One way would be to check the return value:

    do_something_other_than_die() unless defined $l_h;


    Another way would be to wrap it in a eval block (and you shouldn't
    use ampersand on function calls unless you really do want the
    semantics that it brings, see perlsub.pod):

    eval {
    $l_x = $l_h->prepare($l_insert) or
    Error("Prepare $l_insert failed", 'insert');
    }
    if ( [email protected] ) {
    do_something_other_than_die();
    }
     
    Tad McClellan, Nov 17, 2006
    #3
  4. daniel.crosby

    Paul Lalli Guest

    That's because you have a Perl error, not a Database error.
    You shot yourself in the foot, by setting RaiseError to 0 here. If
    you'd set it to 1, Perl would have died when this call to DBI->connect
    failed. Because you set it to 0, and didn't bother checking the return
    value, you have no way of knowing that the connect() failed until you
    later try to erroneously use the return value of the failed connect().
    Here. When DBI->connect() failes, it returned undef to $l_h, but you
    didn't check that possibility. So when you use it here, you're trying
    to call a method on an undefined value, which is a Perl error.

    Paul Lalli
     
    Paul Lalli, Nov 17, 2006
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.