python异常处理
# 如果没有_validated_data,则对initial_data验证,把验证通过的返回值给到_initial_data # 如果验证失败,抛出_validated_data异常,_validated_data = {},把异常给到_errors # 如果没有异常,则self._errors = {} if not hasattr(self, '_validated_data'): try: self._validated_data = self.run_validation(self.initial_data) except ValidationError as exc: self._validated_data = {} self._errors = exc.detail else: self._errors = {}
先执行try内容,通不过,则看下能不能匹配到SyntaxError或者SystemError
如果都匹配不到,则print('unknow error')
try通过,则继续运行else的内容
try: a = b print(a) except SyntaxError as e: print(e) except SystemError as e: print(e) except: print('unknow error') else: print('xx')