Use exceptions instead of calling croak()

This little bit of test code was causing me a lot of grief:

You see the regex for qr/Table.1111111111.doesn't exist/? Due to a slight rewording in the error message, that test kept failing. However, it was failing in a way that the following test used to keep failing. As it turns out, I had fixed a bug these tests were designed to catch but it looked at first like I hadn't fixed the bug. Because of the changed error message (and me misreading the test number), I spent a lot of time trying to track down a bug that did not exist.

If I had been throwing proper exceptions, my tests would be trying to validate the class of the exception, rather than the text of the exception. I could have changed my error messages at will without worrying about breaking my tests. Yet another reason why you usually want exceptions instead of calling die or croak.

1 Comment

Exceptions are awesome, though lacking in perl, and I'm not a fan of Exception::Class, so I wrote Exception::Simple, it's only small and basic, but allows you to do what you state here. I've never looked back since, I throw Exceptions left, right and centre these days!

Leave a comment

About Ovid

user-pic Have Perl; Will Travel. Freelance Perl/Testing/Agile consultant. Photo by http://www.circle23.com/. Warning: that site is not safe for work. The photographer is a good friend of mine, though, and it's appropriate to credit his work.