Skip to Content
Perl To Python Migration
book

Perl To Python Migration

by Martin C. Brown
November 2001
Beginner
320 pages
5h 53m
English
Pearson Business
Content preview from Perl To Python Migration

CREATING NEW EXCEPTION CLASSES

You can create your own exception classes simply by creating a new class which should be set to inherit from one of the existing exception classes. Alternatively, for backwards compatibility, you can also create a string-based exception, for example:

CustomError = 'Error'

raise CustomError

Note that exceptions match against values, thus raising a string exception, but checking against the object will still work:

CustomError = 'Error'

def test():
    raise 'Error'

try:
    test()
except CustomError:
    print "Error!"

However, it's bad practice to rely on this, so you should instead raise one of the predefined error strings.

If you want to create class-based exceptions create a new class, including any initialization information. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Perl in a Nutshell

Perl in a Nutshell

Nathan Patwardhan, Ellen Siever, Stephen Spainhour
Perl by Example

Perl by Example

Ellie Quigley
Perl Hacks

Perl Hacks

Chromatic, Damian Conway, Curtis Ovid Poe, Curtis (Ovid) Poe

Publisher Resources

ISBN: 0201734885Purchase book