### 异常处理#### 基本格式```pythontry: passexcept Exception as e: pass``````pythontry: v = [] v[11111] # IndexErrorexcept ValueError as e: passexcept IndexError as e: passexcept Exception as e: print(e) # e是Exception类的对象,中有一个错误信息。``````try: int('asdf')except Exception as e: print(e) # e是Exception类的对象,中有一个错误信息。finally: print('最后无论对错都会执行') # #################### 特殊情况 #########################def func(): try: # v = 1 # return 123 int('asdf') except Exception as e: print(e) # e是Exception类的对象,中有一个错误信息。 return 123 finally: print('最后')func()```#### 主动触发异常```pythontry: int('123') raise Exception('阿萨大大是阿斯蒂') # 代码中主动抛出异常except Exception as e: print(e)``````pythondef func(): result = True try: with open('x.log',mode='r',encoding='utf-8') as f: data = f.read() if 'alex' not in data: raise Exception() except Exception as e: result = False return result```#### 自定义异常```pythonclass MyException(Exception): passtry: raise MyException('asdf')except MyException as e: print(e)``````pythonclass MyException(Exception): def __init__(self,message): super().__init__() self.message = messagetry: raise MyException('asdf')except MyException as e: print(e.message)```##