Do you use an exception class in your Perl programs? Why or why not?

I don’t use exception objects very often; mostly because a string is usually enough and involves less work. This is because there is usually nothing the program can do about the exception. If it could have avoided the exception, it wouldn’t have caused it in the first place.

If you can do something about the exceptions, use objects. If you are just going to kill the program (or some subset, say, a web request), save yourself the effort of coming up with an elaborate hierarchy of objects that do nothing more than contain a message.

As for number 4; $SIG{__DIE__} should never be used. It doesn’t compose; if one module expects sigdie to work in one way, and another module is loaded that makes it work some other way, those modules can’t be used in the same program anymore. So don’t do that.

If you want to use objects, just do the very-boring die Object->new( ... ). It may not be exciting as some super-awesome magic somewhere, but it always works and the code does exactly what it says.

Leave a Comment