條件檢查:如何編寫安全代碼:條件檢查與異常處理?
if denominator == 0:
// do something like informing the user, or skipping this iteration.
else:
result = numerator/denominator
if FileExists('path/to/file'):
// open file read & write.
else:
// do something like informing the user, or skipping this iteration.
異常處理:
try:
result = numerator/denominator
catch (DevidedByZeroException):
//take action
try:
//open file read & write.
catch (FileNotExistsException):
//take action
我頻頻遭遇的情況是這樣的。哪一個去?爲什麼?