Discuz! BBS

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 725|回复: 0

Python异常处理

[复制链接]

254

主题

363

帖子

2431

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2431
发表于 2024-1-5 11:34:20 | 显示全部楼层 |阅读模式
注意Raise的用法:
  1. #-*- encoding:UTF-8 -*-
  2. import os

  3. def mkPath(sourcePath, destPath):
  4.         # ent1-ls = sourcePath.split('/')[1:]
  5.     # ent2-ls = destPath.split('/')[1:]
  6.         # if os.path.exists(destPath):
  7.         #   return True
  8.         # else:
  9.         try:
  10.                 os.makedirs(destPath)
  11.         except Exception as err:
  12.                 print('mkPath Error:', str(Exception), str(err))  # print(repr(Exception), srepr(err))
  13.                 # raise  # ??如加入这句,则系统抛出错误停止运行。就是说重新抛出错误,上面已经打印的情况下,
  14.                            # 重新向python解释器默认错误处理器抛出错误。
  15.         finally:
  16.                 print('After deal with except, I am doing.')  # 这句在重抛错误之前打印。
  17.         print(destPath + '已建立路径,如无报错则建立成功。')  # except 处理完毕之后,系统继续执行程序。

  18. if __name__ == '__main__':
  19.         sourcePath = 'c:\\nonono'
  20.         destPath = 'n:\\nonono'
  21.         mkPath(sourcePath, destPath)
  22.         print('Up ok .')
复制代码


嵌套的异常:
  1. #-*- encoding:UTF-8 -*-
  2. import os

  3. def mkPath(sourcePath, destPath):
  4.         # ent1-ls = sourcePath.split('/')[1:]
  5.     # ent2-ls = destPath.split('/')[1:]
  6.         # if os.path.exists(destPath):
  7.         #   return True
  8.         # else:
  9.         try:
  10.                 try:
  11.                         os.makedirs(destPath)
  12.                 except IOError as err:
  13.                         print('mkPath Error:', repr(IOError), str(err))  # print(repr(IOError), repr(err)) repr(err)显示效果不好。
  14.                         raise  # ??如加入这句,则系统抛出错误停止运行。就是说重新抛出错误,上面已经打印的情况下,
  15.                                    # 重新向python解释器默认错误处理器抛出错误。注意只抛一层,在外层就接住了。
  16.                 finally:
  17.                         print('After deal with inner except, I am doing.')  # 这句在重抛错误之前打印。
  18.                 print(destPath + '已建立路径,如无报错则建立成功。')  # except 处理完毕之后,系统继续执行程序。
  19.         except Exception as err:
  20.                 print('Im outer exception dealing.')
  21.                 raise  # 这个raise名字就叫做抛,抛毛的抛。

  22. if __name__ == '__main__':
  23.         sourcePath = 'c:\\nonono'
  24.         destPath = 'n:\\nonono'
  25.         mkPath(sourcePath, destPath)
  26.         print('Up ok .')
复制代码


控制台显示如下:
  1. mkPath Error: <class 'OSError'> [WinError 3] 系统找不到指定的路径。: 'n:\\'
  2. After deal with inner except, I am doing.
  3. Im outer exception dealing.

  4. Traceback (most recent call last):
  5.   File "<stdin>", line 4, in <module>
  6.   File "<stdin>", line 9, in mkPath
  7.   File "C:\Python36\lib\os.py", line 210, in makedirs
  8.     makedirs(head, mode, exist_ok)
  9.   File "C:\Python36\lib\os.py", line 220, in makedirs
  10.     mkdir(name, mode)
  11. FileNotFoundError: [WinError 3] 系统找不到指定的路径。: 'n:\\'
复制代码


如果把第二个raise去掉,则输出如下:
  1. mkPath Error: <class 'OSError'> [WinError 3] 系统找不到指定的路径。: 'n:\\'
  2. After deal with inner except, I am doing.
  3. Im outer exception dealing.
  4. Up ok .
复制代码

注意可以通过Exception的派生类捕获特定的异常。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-4-16 09:05 , Processed in 0.012774 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表