捕獲自定義異常

此示例顯示如何捕獲自定義 Exception

class CustomError(Exception):
     pass

try:
    raise CustomError('Can you catch me ?')
except CustomError as e:
    print ('Catched CustomError :{}'.format(e))
except Exception as e:
    print ('Generic exception: {}'.format(e))

輸出:

Catched CustomError :Can you catch me ?