Loginskip to content

October 31st, 2006

Bullet-proof your program by trapping exceptions and handling

# Code to acquire a database connection atexit(&cleanup); # More database code which might fail } This ensures that no matter how that code block is exited, the cleanupsubroutine to break the connection gracefully will be run. 9.5 Confession Is Good for the Soul Sometimes your program dies not in your code but in some module you call suicide by association if you will. A module that wants to die is supposed to call Carp::croakinstead, because croakreports the location of death as the place the module function was called, not the line of code in the module function containing the croakcall. Some modules don’t do this. Either they call die, or they call another module that croaks; either way, you’re left with an error message that doesn’t tell you which line of your program caused its untimely demise. If this happens, here’s a quick way to force it to ‘fess up. Near the beginning of your program, insert the following: use Carp qw(cluck verbose); $SIG{__DIE__} = sub { confess @_ }; $SIG{__WARN__} = sub { cluck @_ }; Now whenever your program calls die/croakor carp/warnit ends up calling confessor cluck[9] instead, which have exactly the same effect except they print a stack trace of how they were called that goes all the way back to your main program. [9] Perl is nothing if not whimsical. Presumably in this case it helps to dispel the air of morbidity surrounding the whole topic of program death. Chapter 10. Semantical Errors “Pay attention even to trifles.” A Book of Five Rings, by Miyamoto Musashi
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

Comments are closed.