FAQ
when the table does not exist the prepare fails.  How do ignore the error thrown by the prepare() and catch it later in the program?


my $dbh = DBI->connect("dbi:ODBC:mysql"
,$login
,$passwd
,{ RaiseError => 0 }
);
...
$sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");   # don't catch
$retCode = $sth->execute();
if ($dbh->err) {
print "Table $table -- DOES NOT EXIST\n";
}
....

Search Discussions

  • 7 at Feb 1, 2010 at 8:06 pm

    On Mon, Feb 1, 2010 at 12:11 PM, Tony Esposito wrote:

    when the table does not exist the prepare fails. How do ignore the error
    thrown by the prepare() and catch it later in the program?


    my $dbh = DBI->connect("dbi:ODBC:mysql"
    ,$login
    ,$passwd
    ,{ RaiseError => 0 }
    );
    ...
    $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable"); # don't catch
    $retCode = $sth->execute();
    if ($dbh->err) {
    print "Table $table -- DOES NOT EXIST\n";
    }
    ....


    See if this helps:


    use strict;
    use warnings;
    use 5.010;


    eval {
    die "***prepare error***";
    };

    say 'executing other code here';

    if ($@) {
    say "There was an error inside that eval block above: $@";
    }


    --output:--
    executing other code here
    There was an error inside that eval block above: ***prepare error*** at
    2perl.pl line 7.
  • Tony Esposito at Feb 1, 2010 at 8:41 pm
    Is this the idea? I do not ever want to catch the error from the prepare statement itself -- I want my code to catch and throw the error.  And I am using Perl 5.8 so I do not need the use 5.010; pragma ....  thx

    use strict;
    use warnings;
    use 5.010;

    eval {
    my $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");
    };

    if ($dbh->err) {
    print "Table $table -- DOES NOT EXIST\n";
    }
    $retCode = $sth->execute();

    --- On Mon, 1/2/10, 7 wrote:


    From: 7 <7stud.7stud@gmail.com>
    Subject: Re: prepare(SELECT ... FROM TABLE) error
    To: "Beginners Perl" <beginners@perl.org>
    Date: Monday, 1 February, 2010, 14:05


    On Mon, Feb 1, 2010 at 12:11 PM, Tony Esposito
    wrote:
    when the table does not exist the prepare fails.  How do ignore the error
    thrown by the prepare() and catch it later in the program?


    my $dbh = DBI->connect("dbi:ODBC:mysql"
    ,$login
    ,$passwd
    ,{ RaiseError => 0 }
    );
    ...
    $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");   # don't catch
    $retCode = $sth->execute();
    if ($dbh->err) {
    print "Table $table -- DOES NOT EXIST\n";
    }
    ....


    See if this helps:


    use strict;
    use warnings;
    use 5.010;


    eval {
    die "***prepare error***";
    };

    say 'executing other code here';

    if ($@) {
    say "There was an error inside that eval block above: $@";
    }









    my $dbh = DBI->connect("dbi:ODBC:mysql"
    ,$login
    ,$passwd
    ,{ RaiseError => 0 }
    );
    ...
    $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");   # don't catch
    $retCode = $sth->execute();
    if ($dbh->err) {
    print "Table $table -- DOES NOT EXIST\n";
    }
    ....


    See if this helps:


    use strict;
    use warnings;
    use 5.010;


    eval {
    die "***prepare error***";
    };

    say 'executing other code here';

    if ($@) {
    say "There was an error inside that eval block above: $@";
    }


    --- On Mon, 1/2/10, 7 wrote:


    From: 7 <7stud.7stud@gmail.com>
    Subject: Re: prepare(SELECT ... FROM TABLE) error
    To: "Beginners Perl" <beginners@perl.org>
    Date: Monday, 1 February, 2010, 14:05


    On Mon, Feb 1, 2010 at 12:11 PM, Tony Esposito
    wrote:
    when the table does not exist the prepare fails.  How do ignore the error
    thrown by the prepare() and catch it later in the program?


    my $dbh = DBI->connect("dbi:ODBC:mysql"
    ,$login
    ,$passwd
    ,{ RaiseError => 0 }
    );
    ...
    $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");   # don't catch
    $retCode = $sth->execute();
    if ($dbh->err) {
    print "Table $table -- DOES NOT EXIST\n";
    }
    ....


    See if this helps:


    use strict;
    use warnings;
    use 5.010;


    eval {
    die "***prepare error***";
    };

    say 'executing other code here';

    if ($@) {
    say "There was an error inside that eval block above: $@";
    }


    --output:--
    executing other code here
    There was an error inside that eval block above: ***prepare error*** at
    2perl.pl line 7.
  • Tony Esposito at Feb 1, 2010 at 11:03 pm
    Also, if prepare fails, I want to continue processing -- hence the need to capture the error in my code and not have DBI::DBD throw and exception then stop all processing ...

    --- On Mon, 1/2/10, Tony Esposito wrote:


    From: Tony Esposito <tony1234567893@yahoo.co.uk>
    Subject: Re: prepare(SELECT ... FROM TABLE) error
    To: "Beginners Perl" <beginners@perl.org>, "7" <7stud.7stud@gmail.com>
    Date: Monday, 1 February, 2010, 14:40


    Is this the idea? I do not ever want to catch the error from the prepare statement itself -- I want my code to catch and throw the error.  And I am using Perl 5.8 so I do not need the use 5.010; pragma ....  thx

    use strict;
    use warnings;
    use 5.010;

    eval {
    my $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");
    };

    if ($dbh->err) {
    print "Table $table -- DOES NOT EXIST\n";
    }
    $retCode = $sth->execute();

    --- On Mon, 1/2/10, 7 wrote:


    From: 7 <7stud.7stud@gmail.com>
    Subject: Re: prepare(SELECT ... FROM TABLE) error
    To: "Beginners Perl" <beginners@perl.org>
    Date: Monday, 1 February, 2010, 14:05


    On Mon, Feb 1, 2010 at 12:11 PM, Tony Esposito
    wrote:
    when the table does not exist the prepare fails.  How do ignore the error
    thrown by the prepare() and catch it later in the program?


    my $dbh = DBI->connect("dbi:ODBC:mysql"
    ,$login
    ,$passwd
    ,{ RaiseError => 0 }
    );
    ...
    $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");   # don't catch
    $retCode = $sth->execute();
    if ($dbh->err) {
    print "Table $table -- DOES NOT EXIST\n";
    }
    ....


    See if this helps:


    use strict;
    use warnings;
    use 5.010;


    eval {
    die "***prepare error***";
    };

    say 'executing other code here';

    if ($@) {
    say "There was an error inside that eval block above: $@";
    }









    my $dbh = DBI->connect("dbi:ODBC:mysql"
    ,$login
    ,$passwd
    ,{ RaiseError => 0 }
    );
    ...
    $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");   # don't catch
    $retCode = $sth->execute();
    if ($dbh->err) {
    print "Table $table -- DOES NOT EXIST\n";
    }
    ....


    See if this helps:


    use strict;
    use warnings;
    use 5.010;


    eval {
    die "***prepare error***";
    };

    say 'executing other code here';

    if ($@) {
    say "There was an error inside that eval block above: $@";
    }


    --- On Mon, 1/2/10, 7 wrote:


    From: 7 <7stud.7stud@gmail.com>
    Subject: Re: prepare(SELECT ... FROM TABLE) error
    To: "Beginners Perl" <beginners@perl.org>
    Date: Monday, 1 February, 2010, 14:05


    On Mon, Feb 1, 2010 at 12:11 PM, Tony Esposito
    wrote:
    when the table does not exist the prepare fails.  How do ignore the error
    thrown by the prepare() and catch it later in the program?


    my $dbh = DBI->connect("dbi:ODBC:mysql"
    ,$login
    ,$passwd
    ,{ RaiseError => 0 }
    );
    ...
    $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");   # don't catch
    $retCode = $sth->execute();
    if ($dbh->err) {
    print "Table $table -- DOES NOT EXIST\n";
    }
    ....


    See if this helps:


    use strict;
    use warnings;
    use 5.010;


    eval {
    die "***prepare error***";
    };

    say 'executing other code here';

    if ($@) {
    say "There was an error inside that eval block above: $@";
    }


    --output:--
    executing other code here
    There was an error inside that eval block above: ***prepare error*** at
    2perl.pl line 7.
  • 7 at Feb 2, 2010 at 8:27 am

    On Mon, Feb 1, 2010 at 1:40 PM, Tony Esposito wrote:

    Is this the idea? I do not ever want to catch the error from the prepare
    statement itsel
    I thought you said the program fails if you don't catch the error? If so,
    and you want your program to continue executing, then you have to catch the
    error the exact moment it happens.



    -- I want my code to catch and throw the error.
    I'm not sure what you are referring to, so here is some information and
    hopefully you will be able to sort out your problem. You catch errors using
    the terribly misnamed 'eval' block. Any error inside the eval block is
    caught by $@. To throw errors, you can use die(). The die() in my example
    was used to mimic prepare() throwing an error.
  • Tony Esposito at Feb 4, 2010 at 2:22 am
    This question has never been answered.  To out it another way, given the code ...

    foreach my $mytable (@mytables) {
    my $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");
    # report error but move on to next table
    }

    how do I ignore the situation/error that DBI throws when $mytable does not exist and just move on to the next table?



    --- On Mon, 1/2/10, Tony Esposito wrote:

    From: Tony Esposito <tony1234567893@yahoo.co.uk>
    Subject: Re: prepare(SELECT ... FROM TABLE) error
    To: "Beginners Perl" <beginners@perl.org>, "7" <7stud.7stud@gmail.com>
    Date: Monday, 1 February, 2010, 17:02

    Also, if prepare fails, I want to continue processing -- hence the need to capture the error in my code and not have DBI::DBD throw and exception then stop all processing ...

    --- On Mon, 1/2/10, Tony Esposito wrote:


    From: Tony Esposito <tony1234567893@yahoo.co.uk>
    Subject: Re: prepare(SELECT ... FROM TABLE) error
    To: "Beginners Perl" <beginners@perl.org>, "7" <7stud.7stud@gmail.com>
    Date: Monday, 1 February, 2010, 14:40


    Is this the idea? I do not ever want to catch the error from the prepare statement itself -- I want my code to catch and throw the error.  And I am using Perl 5.8 so I do not need the use 5.010; pragma ....  thx

    use strict;
    use warnings;
    use 5.010;

    eval {
    my $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");
    };

    if ($dbh->err) {
    print "Table $table -- DOES NOT EXIST\n";
    }
    $retCode = $sth->execute();

    --- On Mon, 1/2/10, 7 wrote:


    From: 7 <7stud.7stud@gmail.com>
    Subject: Re: prepare(SELECT ... FROM TABLE) error
    To: "Beginners Perl" <beginners@perl.org>
    Date: Monday, 1 February, 2010, 14:05


    On Mon, Feb 1, 2010 at 12:11 PM, Tony Esposito
    wrote:
    when the table does not exist the prepare fails.  How do ignore the error
    thrown by the prepare() and catch it later in the program?


    my $dbh = DBI->connect("dbi:ODBC:mysql"
    ,$login
    ,$passwd
    ,{ RaiseError => 0 }
    );
    ...
    $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");   # don't catch
    $retCode = $sth->execute();
    if ($dbh->err) {
    print "Table $table -- DOES NOT EXIST\n";
    }
    ....


    See if this helps:


    use strict;
    use warnings;
    use 5.010;


    eval {
    die "***prepare error***";
    };

    say 'executing other code here';

    if ($@) {
    say "There was an error inside that eval block above: $@";
    }









    my $dbh = DBI->connect("dbi:ODBC:mysql"
    ,$login
    ,$passwd
    ,{ RaiseError => 0 }
    );
    ...
    $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");   # don't catch
    $retCode = $sth->execute();
    if ($dbh->err) {
    print "Table $table -- DOES NOT EXIST\n";
    }
    ....


    See if this helps:


    use strict;
    use warnings;
    use 5.010;


    eval {
    die "***prepare error***";
    };

    say 'executing other code here';

    if ($@) {
    say "There was an error inside that eval block above: $@";
    }


    --- On Mon, 1/2/10, 7 wrote:


    From: 7 <7stud.7stud@gmail.com>
    Subject: Re: prepare(SELECT ... FROM TABLE) error
    To: "Beginners Perl" <beginners@perl.org>
    Date: Monday, 1 February, 2010, 14:05


    On Mon, Feb 1, 2010 at 12:11 PM, Tony Esposito
    wrote:
    when the table does not exist the prepare fails.  How do ignore the error
    thrown by the prepare() and catch it later in the program?


    my $dbh = DBI->connect("dbi:ODBC:mysql"
    ,$login
    ,$passwd
    ,{ RaiseError => 0 }
    );
    ...
    $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");   # don't catch
    $retCode = $sth->execute();
    if ($dbh->err) {
    print "Table $table -- DOES NOT EXIST\n";
    }
    ....


    See if this helps:


    use strict;
    use warnings;
    use 5.010;


    eval {
    die "***prepare error***";
    };

    say 'executing other code here';

    if ($@) {
    say "There was an error inside that eval block above: $@";
    }


    --output:--
    executing other code here
    There was an error inside that eval block above: ***prepare error*** at
    2perl.pl line 7.
  • Dr.Ruud at Feb 5, 2010 at 9:50 am

    Tony Esposito wrote:

    This question has never been answered. To out it another way, given the code ...

    foreach my $mytable (@mytables) {
    my $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");
    # report error but move on to next table
    }

    how do I ignore the situation/error that DBI throws when $mytable does not exist and just move on to the next table?
    Why would you want to?

    Why not just ask the database which tables are there?

    If you really want to do it in an ugly way, then
    first read "perldoc DBI" about "err" and "errstr"
    and "RaiseError", etc.

    --
    Ruud
  • Jay Savage at Feb 9, 2010 at 6:42 pm

    On Wed, Feb 3, 2010 at 9:22 PM, Tony Esposito wrote:
    This question has never been answered.  To out it another way, given the code ...

    foreach my $mytable (@mytables) {
    my $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");
    # report error but move on to next table
    }

    how do I ignore the situation/error that DBI throws when $mytable does not exist and just move on to the next table?


    The question was answered, but perhaps you did not understand the answer:

    Wrap the call in an eval block. Then check $@ to see if there was a
    fatal error, which you can ignore if you want to or do something along
    the lines of:

    eval {
    my $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");
    };

    if ($@) {
    print "Table $table -- (probably) DOES NOT EXIST\n";
    next;
    }

    $retCode = $sth->execute();


    HTH,

    -- jay
    --------------------------------------------------
    This email and attachment(s): [ ] blogable; [ x ] ask first; [ ]
    private and confidential

    daggerquill [at] gmail [dot] com
    http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org

    values of β will give rise to dom!
  • Dr.Ruud at Feb 10, 2010 at 1:05 am

    Jay Savage wrote:

    Wrap the call in an eval block. Then check $@ to see if there was a
    fatal error, which you can ignore if you want to or do something along
    the lines of:

    eval {
    my $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");
    };

    if ($@) {
    print "Table $table -- (probably) DOES NOT EXIST\n";
    next;
    }
    Because $@ is a global, it is best practice to act on
    the return value of eval itself:

    my $sth;
    eval {
    $sth = $dbh->prepare("SELECT COUNT(*) FROM mytable");
    1; # success
    }
    or do {
    my $eval_error = $@ || "unknown";
    print $eval_error, "\n";
    print "Table $table -- (probably) DOES NOT EXIST\n";
    next;
    };


    But it is still much better to just ask for the table names,
    like with "SHOW TABLES LIKE '...'", or with something like

    SELECT
    table_name AS _tbl
    FROM
    information_schema.tables
    WHERE
    table_schema = "db"
    AND
    table_name REGEXP "^abc_"
    ;

    --
    Ruud
  • Jay Savage at Feb 10, 2010 at 6:03 pm

    On Tue, Feb 9, 2010 at 8:05 PM, Dr.Ruud wrote:
    Jay Savage wrote: [snip]
    Because $@ is a global, it is best practice to act on
    the return value of eval itself:
    [snip]

    $@ is also *guaranteed*--in the words of perlfunc--to be set
    correctly. I believe that historically this may not have been the
    case: $@ may have only been set on failure and not flushed on success,
    but in recent Perls it should be reliable.

    or do {
    my $eval_error = $@ || "unknown";
    print $eval_error, "\n";
    This seems superfluous. If you don't trust $@ to be set correctly, why
    interrogate it? I'm also curious under what circumstances you believe
    the eval could fail but $@ not be true (i.e., when would you expect
    your code to print "unknown"?).

    -- j
  • Jeff Peng at Feb 10, 2010 at 10:32 pm

    On Thu, Feb 11, 2010 at 2:03 AM, Jay Savage wrote:
    $@ is also *guaranteed*--in the words of perlfunc--to be set
    correctly. I believe that historically this may not have been the
    case: $@ may have only been set on failure and not flushed on success,
    but in recent Perls it should be reliable.
    I also agree.
    I'm also curious under what circumstances you believe
    the eval could fail but $@ not be true (i.e., when would you expect
    your code to print "unknown"?).
    I think Ruud didn't mean eval fail but $@ not be true.
    If I understand for that correctly, what he tried to explain is, under
    some cases the $@ is set by other eval {} call, not the current eval
    {} you followed. Though I don't know if there is the case it will
    happen...


    Jeff.
  • Dr.Ruud at Feb 11, 2010 at 1:21 am

    Jay Savage wrote:
    Dr.Ruud:
    Because $@ is a global, it is best practice to act on
    the return value of eval itself:
    [snip]

    $@ is also *guaranteed*--in the words of perlfunc--to be set
    correctly. I believe that historically this may not have been the
    case: $@ may have only been set on failure and not flushed on success,
    but in recent Perls it should be reliable.
    And then it isn't. Try a die in a DESTROY handler for example.

    See also discussions about @@ on p5p.

    --
    Ruud

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedFeb 1, '10 at 7:11p
activeFeb 11, '10 at 1:21a
posts12
users5
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase