$SIG{__DIE__} = sub { print STDERR @_; print STDOUT ...; # whatever you want exit(1); }; #### our $::error_context; { local $::error_context = "RC-E"; $dbh->prepare(...); # dbh call in the RC-E context { local $::error_context = "RC-F"; $dbh->whatever(...); # dbh call in the RC-F context } # back to context "RC-E" ... }