[Tutor] class question

Tiger12506 keridee at jayco.net
Fri Jul 13 21:09:04 CEST 2007


> Your second way seems to make more sense. And instead of raising the 
> error, why not just print it:

There is a very good reason for this and it's important that you understand 
it to write good code. If you use a print statement, you break the benefit 
of encapsulation.

If you were to use that class in a GUI application, for example, you would 
never know if the account had become overdrawn. Only by using an exception 
could you use that class effectively in both circumstances, and in the same 
way!

a = BankAccount()
try:
  a.withdraw(50)
except:
  notify_error()

Where notify error depends on how you want to communicate that information 
to the end-user. (messagebox, display, stdout, stderr, file, etc)

JS 



More information about the Tutor mailing list