Movatterモバイル変換


[0]ホーム

URL:


How to effectively use exception

Steven Haryantosteven at haryan.to
Sat Apr 14 07:18:37 EDT 2001


So far I have understood one rule of thumb about exceptions:"Catch as few of them as possible in the inner blocks of theprogram. The uncaught exceptions will creep up to the upperblocks until, at one level, it will be caught by the code thatknow how to handle it. The main block should catch all therest and act accordingly.There are two things that I seek advice on:First, should we use exceptions whenever possible? For example,should a smallish function that do some testing return 1/0 toindicate success/failure or should it raise ValueError if thetest fails? Is the try-except-else style more Pythonic/recommended than the if-else-then style? If not, what are theconsiderations to balance the two?Second, when do we create our own specific Exception classes,and when do we just use the existing standard Exception (I useValueError in a lot of my methods & functions, accompanied bythe specific message string. So far I haven't faced with theneed to create a custom Exception class).Lastly, can someone recommend some readings on this matter?Thanks, and a happy easter holiday to everybody,Steve


More information about the Python-listmailing list

[8]ページ先頭

©2009-2025 Movatter.jp