rawat011 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I have a perl module file which uses large number of different types exceptions. what I mean by it, if there is a failure then it can throw any of those exception. Now, what I want is, it to throw only one type of exception whatever the error may be. This is needed because i wanted to collect some info in case this file throws an exception. The info collected is same irrespective of exception thrown. Information is collected by method defined on that exception. Now the problem is some of these exceptions also are used by other modules. Information that needs to be collected is different for each of the module.

Solution that I thought of:

I can put everything in try/catch block and throw only one type of exception. Also, subclass this exception from each of the exceptions that module is using. But it is going to be very messy and i dont like the idea.

Please let me know if there is a better way to do it. I want a solution which will require minimal changes to existing code

Replies are listed 'Best First'.
Re: Need help with exceptions
by AppleFritter (Vicar) on Sep 05, 2014 at 11:59 UTC

    Howdy rawat011, welcome to the Monastery!

    What exception handling mechanism are you using Perl has neither try ... catch nor different exception classes built in, so the answer to your question will depend on what module is providing these.

    For instance - based on my reading of the respective docs, Error has an "otherwise" construct that can be used to catch exceptions not caught by specific catch blocks. Exception::Class requires you to peek at an exception's class anyway, so you'd just need to not do that. TryCatch allows condition-less catch blocks that will always trigger for any exception. Try::Tiny's catch blocks also catch any exception by default. And so on.

    Without knowing what your code uses, I'm afraid I can't say more. So, yeah - what module are you using that provides exception handling?

      We use Error module to handle exceptions.