: : Does Perl have a try/catch syntax for exceptions?
: If Perl has a weak spot I can pick at, this might be it. There is a way to throw an exception (using the die keyword), and this propogates up to the next eval { ... } block and causes that to fail. You can check the return value of the eval block to see if an exception was caught, and if it was you can handle it as you wish. This works for both user thrown exceptions and ones that Perl throws. If you don't have a handler, it propogates to the top and Perl spits out the error and terminates.
:
: The main weakness is that there isn't any kind of exception object or any standardised way of saying what went wrong. You are dependent mainly on text based messages, which you set and then have to parse later - a problem. You might well be able to define your own exception object, mind...and there may be modules that handle exception stuff better that you can use.
:
: On the other hand, Perl does allow you to put short circuit OR operators to good use for simple error handling:-
:
: open my $fh, "somefile.txt" || die "Couldn't open file - $!\n";
:
: Good for simple stuff - probably for small scripting tasks, which is where Perl was heavily used in the early days of it's existence.
:
: I suspect that things will be much improved on this front in Perl6. I certainly hope they will be.
Python has great exception handling. You can define your own exceptions just by creating an empty subclass of Exception.
>>> class MyError(Exception): pass
Then you just use the raise command anywhere to "throw" it.
>>> raise MyError
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
MyError
You can pass it an arbitrary string, too:
>>> raise MyError("foo!")
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
MyError: foo!
There are try/catch blocks too:
>>> try:
... raise MyError
... except:
... print "caught exception"
...
caught exception
There's even an "else" block that will execute if the try block
didn't raise an exception.
>>> try:
... pass
... except MyError:
... print "caught exception"
... else:
... print "didn't catch exception"
...
didn't catch exception
You can also catch specific exceptions:
>>> try:
... raise ZeroDivisionError
... except MyError:
... print "caught MyError"
... except ZeroDivisionError:
... print "caught ZeroDivisionError"
... except:
... print "caught error"
...
caught ZeroDivisionError
Also, instead of a generic message, you can catch the instance of your exception too:
>>> try:
... raise MyError("supercalifragilisticexpialidocious")
... except MyError, e:
... print "MyError:", e
...
MyError: supercalifragilisticexpialidocious
There's also an alternative "finally" block to do cleanup before an exception gets raised up the call stack:
>>> try:
... raise MyError("foobar")
... finally:
... print "Cleaning up..."
...
Cleaning up...
Traceback (most recent call last):
File "<interactive input>", line 2, in ?
MyError: foobar
So you can see that Python's exception handling is easy to work with, very flexible, and an integral part of the language.
infidel