I’m playing around with error handling and got a little problem.
I connect with a database using the DBI module.
I do my own error handling by using a subroutine that I call upon an error.
I can catch my own dies and handle them just fine but when my database connection fails, the DBI module apparently prints out it’s own die :
DBI connect(…) failed: ORA-12154: TNS:could not resolve the
connect identifier specified (DBD ERROR: OCIServerAttach) at …
How would I go about catching this ?
I tried using $SIG{__DIE__} like so :
local $SIG{__DIE__} = sub {
my $e = shift;
print "Error: " .$e;
};
This is on the bottom of my main file, in this file I also call the connect subroutine that is available in a module of my own. I also tried putting this piece of code on the bottom of my module but it still prints the error without the
Error:
in front of it.
Okay, found the solution, apparently I needed
__WARN__instead of__DIE__and this piece of code needed to be in the top of the file, before where the error was thrown, unlike the example I read stated 🙂