Can you elaborate on what you mean by 'meaningfully'? I am using eval to report any DB errors and because execute and certain statement errors were not being caught by it I am using die and $!. I understand $! is basic, if you can give an example of how the error handling code could be written more meaningfully, that would help a beginner like myself.
| [reply] |
Can you elaborate on what you mean by 'meaningfully'?
In a way that has meaning, makes sense, conveys information, is useful.
I understand $! is basic
Actually, in this situation, it's value is meaningless, makes no sense, conveys no information, useless.
if you can give an example of how the error handling code could be written more meaningfully
Sorry, I thought the linked document had this.
I prefer to pass RaiseError => 1 to connect. Otherwise, if you establish there has been a DBI error, you can get it using DBI->errstr. You can also call errstr on database and statement handles if you want to make sure to get the right error when dealing with multiple handles.
my $id = $dbh->last_insert_id();
die "Foo: " . $dbh->errstr()
if !defined($id);
| [reply] [d/l] [select] |
Personally, I wouldn't wrap it in an eval block. I'd either enable RaiseError on the $dbh handle and let the module tell me when there was an error, or I'd manually check the return status of each db call and die if needed.
The advantage of letting the module handle the error checking/handling is that it's less code that I have to write while still retaining a reasonable level of error handling. The advantage of manually checking each call is that I have more control over how I want to deal with the error handling.
my $dbh = DBI->connect("dbi:ODBC:$DSN", {RaiseError => 1})
or die "DB connection failure\n" . $DBI::errstr;
| [reply] [d/l] |