Here’s a description of my problem:
I have the task to take a bunch of tablenames and put a prefix in front of them, like so:
PREFIX = 'foo_';
prefixed_tablename = "".join([PREFIX, tablename[:27]])
(The tablename must never exceed 30 characters (some Oracle DB restriction), this is why I only take the first 27 chars).
Now, sometimes this can lead to duplicate tablenames (if only the last 3 chars of a tablename differ).
I could implement some fancy algorithm for creating unique tablenames but at the moment detecting duplicate names would be sufficient. So I thought about storing them in a set, and if creating a prefixed tablename, check the set to see if such a tablename already exists.
Now, for the real problem:
If a duplicate gets detected, I need to stop my script executing, showing some kind of error. In Java, I would just raise an exception, but I don’t know if this would be the preferred way in Python.
Should I raise an exception, or just print out a message and exit?
27 + 4 = 31.
Why wouldn’t you use an exception? If you later don’t want to just exit, but instead catch the exception and do something with it at an outer scope, you’ve got less to change than if you use
sys.exit.