Python异常处理(下)

2021-06-18 18:08:45 浏览数 (1)

三.实例

  1. def boxPrint(symbol, width, height):
  2. if len(symbol) != 1:
  3. raise Exception('Symbol must be a single character string.')
  4. if width <= 2:
  5. raise Exception('Width must be greater than 2.')
  6. if height <= 2:
  7. raise Exception('Height must be greater than 2.')
  8. print(symbol * width) #打印
  9. for i in range(height - 2):
  10. print(symbol (' ' * (width - 2)) symbol)
  11. print(symbol * width)
  12. for sym, w, h in (('*', 4, 4), ('O', 20, 5), ('x', 1, 3), ('ZZ', 3, 3)):
  13. try:
  14.         boxPrint(sym, w, h)
  15. except Exception as err: #返回自定义错误
  16. print('An exception happened: ' str(err))

0 人点赞