此系列更新《Python Real World Data Science》的閱讀摘記,每周六更新祠墅。
全書介紹Python在數(shù)據(jù)科學領域中的應用滋将,分為四模塊:
- Python 基礎
- 數(shù)據(jù)分析
- 數(shù)據(jù)挖掘
- 機器學習
本文為系列第五篇,介紹python基礎。
Module 1 Python Fundamentals
Chapter 5 異常捕獲
Raise
class EvenOnly(list): def append(self, integer): if not isinstance(integer, int): raise TypeError("Only integers can be added") if integer % 2: raise ValueError("Only even numbers can be added") super().append(integer)
Exception Raise之后程序立即停止執(zhí)行
Handle
def funny_division3(anumber): try: if anumber == 13: raise ValueError("13 is an unlucky number") return 100 / anumber except ZeroDivisionError: return "Enter a number other than zero" except TypeError: return "Enter a numerical value" except ValueError: print("No, No, not 13!") raise
Exception Hierarchy
明確指出要捕獲哪一些代碼
Self-defined exceptions
繼承Exception類即可
異持莸埽可用于Flow Control