in reply to Advantage of Exception handling in perl

The old approach: returning special error values or setting some kind of flag basically s**ks. The biggest problem with this way is that you can ignore the errors (you don't even have to do anything for that!). Take for example printing to a file. It's too easy not to check for errors and assume everything went fine. And if you do handle the errors this way you can get a mix-up of error handling code with code that does the actual work. Also if you want to give the caller a chance to recover from the error this might be impossible. All the caller will get is a flag indicating the error or some string.

The exception handling mechanism is much more elegant. It gives a better way of handling errors; you can not ignore errors but are forced do handle them, it leads to more maintainable code and you can basically return * any * type of data you want. Errors might be related, you could set up a hierarchy of exceptions. Try do that the old fashioned way. IMO exception handling is the superior way of error handling. Look at languages like Ada (one of the first with this feature), C++/# or Java. They all have it and for good reason.

  • Comment on Re: Advantage of Exception handling in perl