eeechoo / blogs

个人博客
0 stars 0 forks source link

Errors and Exceptions in Python #10

Open eeechoo opened 5 years ago

eeechoo commented 5 years ago

1. SyntaxError

try:
    x===x
except SyntaxError:
    print "You cannot do that"

这段代码运行时仍然会报 SyntaxError,好像 try except 语句并未能捕获这个异常,这是为什么呢? stackoverflow回答 stackoverflow回答 这里的两个回答涉及 python 解释器的工作原理,我对这里不是太清楚。

2. Exceptions

Errors detected during execution are called exceptions.
-The Python Tutorial - 8. Errors and Exceptions

Python 3.7 exception hierarchy

BaseException
 +-- SystemExit
 +-- KeyboardInterrupt
 +-- GeneratorExit
 +-- Exception
      +-- StopIteration
      +-- StopAsyncIteration
      +-- ArithmeticError
      |    +-- FloatingPointError
      |    +-- OverflowError
      |    +-- ZeroDivisionError
      +-- AssertionError
      +-- AttributeError
      +-- BufferError
      +-- EOFError
      +-- ImportError
      |    +-- ModuleNotFoundError
      +-- LookupError
      |    +-- IndexError
      |    +-- KeyError
      +-- MemoryError
      +-- NameError
      |    +-- UnboundLocalError
      +-- OSError = WindowsError = IOError (windows specified)
      |    +-- 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
eeechoo commented 5 years ago
while True:
print(1)
# IndentationError: expected an indented block
a = 1
if a === 3:
    print(a)
# SyntaxError: invalid syntax
eeechoo commented 5 years ago
print(a)
a = 2
# NameError: name 'a' is not defined
def foo():
    print(a)
    a = 2

foo()
# UnboundLocalError: local variable 'a' referenced before assignment
eeechoo commented 5 years ago
def foo(end):
    a = 0
    while True:
        if a == end:
            break
        yield a
        a += 1

gene = foo(2)
print(next(gene))
print(next(gene))
print(next(gene))
# StopIteration

GeneratorExit异常

eeechoo commented 5 years ago
# 在命令行中运行,然后按下 Control + C
while True:
    print(1)
# KeyboardInterrupt
eeechoo commented 5 years ago

NotImplemented

NotImplementedError

参考内容1博客 参考内容2博客

eeechoo commented 5 years ago

SystemExit

知乎回答

eeechoo commented 5 years ago
  1. EOF 是什么 stackoverflow回答

  2. 什么会引起 EOF

    
    while True:
    a = input("echo: ")
    print(a, type(a))

echo: [1]

[1] <class 'str'>

echo: 123

123 <class 'str'>

echo: ^D

Traceback (most recent call last):

File "C:/Users/PycharmProjects/test123/code reformat.py", line 2, in

a = input("echo: ")

EOFError: EOF when reading a line

eeechoo commented 5 years ago

3. requests.exceptions

hierarchy Python3.7 windows64

https://www.jianshu.com/p/f712a5a46e95