python 自定义异常【raise】
# 抛出一个自定义异常
class MyCustomException(Exception):
pass
def fetch_data_from_api(url):
# 模拟从API获取数据,这里可能会抛出异常
import requests
response = requests.get(url)
if response.status_code == 200:
raise MyCustomException("返回码异常")
return response.json()