VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > python爬虫 >
  • python爬虫之python中的异常处理

本站最新发布   Python从入门到精通|Python基础教程
试听地址  
https://www.xin3721.com/eschool/pythonxin3721/


*这个世界本就不完美......

**就像我们都会在最美好的年华遇到最糟糕的爱情一样

***python程序中不可避免会遇到异常情况

****这时就需要异常处理机制

 

Solution1

try-except[as reason]一旦在某处出错了,之后的程序代码不会被执行了,可以添加else

>>> try:
...     num = input(int(input('please input an integer:')))
... except ValueError as reason:#这里的reason实际上就是内置的reason
...     print(str(reason))#这里可以添加自己其他的说明
... else:#如果前面遇到exception的话这里就不执行了
...     print('no any exception!')
... 
please input an integer:q
invalid literal for int() with base 10: 'q'

Solution2

try-except-finally,finally后面的代码一定会被执行执行的;某处出现了异常,finally下面的代码依旧会继续执行

>>> try:
...     num = input(int(input('please input an integer:')))
... except ValueError as reason:
...     print(str(reason))
... finally:
...     print('this clause must be excuted!')
... 
please input an integer:1
1
this clause must be excuted!

Solution3

raise抛出异常,可以自定义一个异常,但是必须是已经存在的异常类型,只能修改提示的信息

>>> name = 'andox'
>>> if name == 'andy':
...     print('this is a good name!')
... else:
...     raise NameError('Ooops,bad name!')
... 
Traceback (most recent call last):#沿调用关系回溯
  File "<stdin>", line 4, in <module>#描述具体出问题的位置
NameError: Ooops,bad name!

Solution4

try-except语句将捕获所有异常,因为不能具体识别异常,因此此方法不被推荐!

Appendix:Built-in Exceptions

BaseException
 +-- SystemExit
 +-- KeyboardInterrupt
 +-- GeneratorExit
 +-- Exception
      +-- StopIteration
      +-- StopAsyncIteration
      +-- ArithmeticError
      |    +-- FloatingPointError
      |    +-- OverflowError
      |    +-- ZeroDivisionError
      +-- AssertionError
      +-- AttributeError
      +-- BufferError
      +-- EOFError
      +-- ImportError
      |    +-- ModuleNotFoundError
      +-- LookupError
      |    +-- IndexError
      |    +-- KeyError
      +-- MemoryError
      +-- NameError
      |    +-- UnboundLocalError
      +-- OSError
      |    +-- BlockingIOError
      |    +-- ChildProcessError
      |    +-- ConnectionError
      |    |    +-- BrokenPipeError
      |    |    +-- ConnectionAbortedError
      |    |    +-- ConnectionRefusedError
      |    |    +-- ConnectionResetError
      |    +-- FileExistsError
      |    +-- FileNotFoundError
      |    +-- InterruptedError
      |    +-- IsADirectoryError
      |    +-- NotADirectoryError
      |    +-- PermissionError
      |    +-- ProcessLookupError
      |    +-- TimeoutError
      +-- ReferenceError
      +-- RuntimeError
      |    +-- NotImplementedError
      |    +-- RecursionError
      +-- SyntaxError
      |    +-- IndentationError
      |         +-- TabError
      +-- SystemError
      +-- TypeError
      +-- ValueError
      |    +-- UnicodeError
      |         +-- UnicodeDecodeError
      |         +-- UnicodeEncodeError
      |         +-- UnicodeTranslateError
      +-- Warning
           +-- DeprecationWarning
           +-- PendingDeprecationWarning
           +-- RuntimeWarning
           +-- SyntaxWarning
           +-- UserWarning
           +-- FutureWarning
           +-- ImportWarning
           +-- UnicodeWarning
           +-- BytesWarning
           +-- ResourceWarning

相关教程