exception handing

Chris Liechti cliechti at gmx.net
Sat Jun 29 12:52:55 EDT 2002


Rhymes <rhymes at myself.com> wrote in 
news:pan.2002.06.29.17.41.28.926443.440 at myself.com:
...
>>>> ex1 = "spam"
>>>> try:
> ...    raise ex1
> ... except ex1:
> ...    print 'got it'
> ...
> got it
...

exceptions are instances of classes nowdays... don't use strings. and avoid 
troubles...

>>> class Ex1(Exception): pass	#inherit from the root of all exceptios
... 
>>> try: raise Ex1("hello")     #raise instances
... except Ex1: print "got it"  #catch by specifying classes
... 
got it
>>> try: raise Ex1("hello")
... except Exception: print "got it"    	#baseclass works too
... 
got it

as you can see, you have much finer control with classes than strings as 
exceptions. e.g. you can catch the baseclass etc...

chris
-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list