I haven’t introduced much about error messages so far, but they have been used in some examples. Python has at least two types of errors: Syntax errors and Exceptions
Syntax errors, also called parsing errors, are Python A common complaint among beginners.
>>> while True print('Hello world') File "<stdin>", line 1while True print('Hello world') ^SyntaxError: invalid syntax
The Python parser repeatedly prints the error line and displays a small arrow pointing to the position in the error line where the error was first detected. The error is caused by the statement before the arrow (at least that's what is detected): In the above example, the error is detected in the print()
function because of the colon :## before it # is missing. The file name and line number are also printed, making it easier to locate problems when the program comes from a script.
Exceptions. Exceptions are not absolutely fatal errors: Next, we will introduce how to handle exceptions in Python. However, most exceptions will not be handled by the program, and they will eventually become error messages, as shown below:
>>> 10 * (1/0) Traceback (most recent call last): File "<stdin>", line 1, in <module>ZeroDivisionError: division by zero>>> 4 + spam*3Traceback (most recent call last): File "<stdin>", line 1, in <module>NameError: name 'spam' is not defined>>> '2' + 2Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: Can't convert 'int' object to str implicitly
ZeroDivisionError, NameException
NameError and TypeException
TypeError. The string printed as the exception type is the name of the built-in exception. This is true for all built-in exceptions, and although this convention is useful, not all user-defined exceptions will adhere to it. Standard exception names are built-in identifiers (not reserved words).
Control-C or other means supported by the operating system); note that user-induced interruptions will throw a KeyboardInterrupt exception.
while True:try: x = int(input("Please enter a number: "))breakexcept ValueError:print("Oops! That was no valid number. Try again...")
tryThe execution order of statements is:
try and The
try clause between except
except clause , tryStatement execution completed
except, then the except clause is executed, and then the statement after the
try statement continues.
try statement; if the exception cannot be handled, the exception becomes
Unhandled exception, causing the program to terminate and print a message like the example above.
try statement can have multiple except clauses to specify handling methods for different exceptions. At most one except clause will be executed. The handler will only handle exceptions that occur in the corresponding try clause, and will not handle exceptions that occur in other handlers in the same
try statement. An except clause can list multiple exceptions using a parenthesized tuple, like this:
... except (RuntimeError, TypeError, NameError): ... pass
except The classes in the clause can match types that are Exceptions of its subclasses or its own type (but not the other way around, subclasses in the except clause will not match parent class exceptions). For example, the following code will print B, C, D in sequence:
class B(Exception):passclass C(B):passclass D(C):passfor cls in [B, C, D]:try:raise cls()except D:print("D")except C:print("C")except B:print("B")
except B comes first), then it will Print B, B, B - The first except clause match is triggered.
import systry: f = open('myfile.txt') s = f.readline() i = int(s.strip())except OSError as err:print("OS error: {0}".format(err))except ValueError:print("Could not convert data to an integer.")except:print("Unexpected error:", sys.exc_info()[0])raise
try...
except statement There is an optional
else clause, which can only appear after all except clauses. If a section of code must be executed when the try clause does not throw an exception, it is useful to use the else clause (Translation Note: else clause will be
return, break, continue skipped). For example:
for arg in sys.argv[1:]:try: f = open(arg, 'r')except OSError:print('cannot open', arg)else:print(arg, 'has', len(f.readlines()), 'lines') f.close()
使用 else
子句比在 try
子句中附加代码要好,因为这样可以避免 try
... except
意外的捕获的本不属于它们保护的那些代码抛出的异常。
异常发生时,可以携带与之关联的值,也称作异常参数。异常参数是否可以存在以及其类型取决于异常类型。
except子句可以在异常名字后指定变量。该变量绑定到异常实例,异常参数存储在instance.args
属性中。方便起见,异常实例定义了__str__()
以便异常参数可以直接打印,而不是使用.args
引用。也可以首先实例化异常,并在抛出它之前加入任何想要的参数:
>>> try: ... raise Exception('spam', 'eggs') ... except Exception as inst: ... print(type(inst)) # the exception instance... print(inst.args) # arguments stored in .args... print(inst) # __str__ allows args to be printed directly,... # but may be overridden in exception subclasses... x, y = inst.args # unpack args... print('x =', x) ... print('y =', y) ...<class 'Exception'>('spam', 'eggs') ('spam', 'eggs') x = spam y = eggs
对于未处理异常来说,如果它有参数,那么参数将在错误信息的后面部分(详细信息部分)打印。
异常处理程序不仅仅会处理即时发生在try子句中的异常,还会处理try子句中直接或者间接调用的函数中发生的异常。例如:
>>> def this_fails(): ... x = 1/0...>>> try: ... this_fails() ... except ZeroDivisionError as err: ... print('Handling run-time error:', err) ... Handling run-time error: division by zero
raise语句允许程序员强制发生异常。例如:
>>> raise NameError('HiThere') Traceback (most recent call last): File "<stdin>", line 1, in <module>NameError: HiThere
raise
的唯一参数指定要抛出的异常。参数必须是异常实例或者异常类(继承自Exception类的子类)。如果参数是异常类型,会隐式使用无参方式调用异常的构造器初始化一个异常实例:
raise ValueError # shorthand for 'raise ValueError()'
如果需要捕获异常但是不处理,一种更简单形式的raise
语句允许重新抛出这个异常:
>>> try: ... raise NameError('HiThere') ... except NameError: ... print('An exception flew by!') ... raise... An exception flew by!Traceback (most recent call last): File "<stdin>", line 2, in <module>NameError: HiThere
程序中可以通过创建新异常类的方式提出自己的异常(参见Classes
获取Python类的更多信息)。异常必须直接或者间接继承自Exception
类。
自定义异常类拥有其他类的功能,但通常需要保持其简洁性,只提供几个供异常处理程序提取错误信息的属性。需要创建一个抛出若干不同错误的模块时,比较好的实践是,为定义在这个模块中的异常创建父类,由子类创建对应不同错误的具体异常:
class Error(Exception):"""Base class for exceptions in this module."""passclass InputError(Error):"""Exception raised for errors in the input. Attributes: expression -- input expression in which the error occurred message -- explanation of the error """def __init__(self, expression, message):self.expression = expressionself.message = messageclass TransitionError(Error):"""Raised when an operation attempts a state transition that's not allowed. Attributes: previous -- state at beginning of transition next -- attempted new state message -- explanation of why the specific transition is not allowed """def __init__(self, previous, next, message):self.previous = previousself.next = nextself.message = message
与标准异常类类似,大多数异常的名字都以"Error"结尾。
许多标准模块都定义了自己的异常,这些异常对应模块中定义的函数中可能发生的错误。更多信息参考Classes。
try
语句有可选的在任何情况下都会执行的子句,可用于定义清理动作。例如:
>>> try: ... raise KeyboardInterrupt... finally: ... print('Goodbye, world!') ... Goodbye, world!KeyboardInterruptTraceback (most recent call last): File "<stdin>", line 2, in <module>
无论是否有异常发生,finally子句在离开try
语句之前总是会执行。当try
子句中有异常发生并且没有被except
子句处理(或者异常发生在except
子句或else
子句),finally
子句执行之后,这些异常会重新抛出。当try
语句的其他子句通过break, continue
或者return
语句离开时,finally
子句也会执行。以下是较为复杂的示例:
>>> def divide(x, y): ... try: ... result = x / y ... except ZeroDivisionError: ... print("division by zero!") ... else: ... print("result is", result) ... finally: ... print("executing finally clause") ...>>> divide(2, 1) result is 2.0executing finally clause>>> divide(2, 0) division by zero!executing finally clause>>> divide("2", "1") executing finally clause Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in divideTypeError: unsupported operand type(s) for /: 'str' and 'str'
正如所见的那样,finally
子句在任何情况下都会执行。两个字符串相除产生的TypeError
异常没有被except
子句处理,因此在finally
子句执行完毕之后被重新抛出。
在实践应用中,finally
子句用来释放外部资源(比如文件和网络连接),不论资源的使用是否成功。
一些对象定义了标准的清理动作,当不再需要这些对象时,会执行清理动作,而不论使用对象的操作是否成功。以下示例读取文件并打印内容到显示器:
for line in open("myfile.txt"):print(line, end="")
这段代码的问题是:当代码执行完毕后,文件会保留打开状态一段不确定的时间。这在简单的脚本中不是什么大问题,但是在大型的应用程序中会出问题。with
语句使得像文件一样的对象可以被立即准确回收。
with open("myfile.txt") as f:for line in f:print(line, end="")
语句执行后,即使在执行代码时遇到问题,文件f总是会被关闭。像文件一样提供预定义清理动作的对象会在其说明文档中指示这点。
The above is the detailed content of Introduction to two error types in Python. For more information, please follow other related articles on the PHP Chinese website!