Off topic board

Moderators: leeos
Number of threads: 560
Number of posts: 3925

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Re: F.I of the month Posted by infidel on 17 Nov 2003 at 1:56 PM
: : 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

Thread Tree
infidel F.I of the month on 17 Nov 2003 at 7:21 AM
lionb Re: F.I of the month on 17 Nov 2003 at 8:05 AM
DarQ LOL @ mr infidel !!!! <n/t> on 17 Nov 2003 at 9:47 AM
Jonathan Re: F.I of the month on 17 Nov 2003 at 11:11 AM
infidel Re: F.I of the month on 17 Nov 2003 at 12:27 PM
lionb Re: F.I of the month on 17 Nov 2003 at 12:42 PM
Jonathan Re: F.I of the month on 17 Nov 2003 at 12:51 PM
lionb Re: F.I of the month on 17 Nov 2003 at 1:11 PM
lionb Re: F.I of the month on 17 Nov 2003 at 1:20 PM
infidel Re: F.I of the month on 17 Nov 2003 at 1:36 PM
lionb Re: F.I of the month on 18 Nov 2003 at 6:06 AM
lionb Re: F.I of the month on 18 Nov 2003 at 7:00 AM
infidel Re: F.I of the month on 18 Nov 2003 at 7:43 AM
bruce.armstron Re: F.I of the month on 19 Jan 2004 at 1:32 PM
lionb Re: F.I of the month on 21 Jan 2004 at 5:20 AM
bruce.armstron Re: F.I of the month on 21 Jan 2004 at 7:24 AM
lionb Re: F.I of the month on 21 Jan 2004 at 8:53 AM
Jonathan Re: F.I of the month on 17 Nov 2003 at 12:50 PM
infidel Re: F.I of the month on 17 Nov 2003 at 1:56 PM



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.