# SOME DESCRIPTIVE TITLE. # Copyright (C) 2001-2022, Python Software Foundation # This file is distributed under the same license as the Python package. # # Translators: # jerrychen , 2016 # Steven Hsu , 2021-2022 # Matt Wang , 2021 msgid "" msgstr "" "Project-Id-Version: Python 3.12\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2023-07-06 16:53+0000\n" "PO-Revision-Date: 2022-10-24 14:54+0800\n" "Last-Translator: Steven Hsu \n" "Language-Team: Chinese - TAIWAN (https://github.com/python/python-docs-zh-" "tw)\n" "Language: zh_TW\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=1; plural=0;\n" "X-Generator: Poedit 3.2\n" #: ../../tutorial/errors.rst:5 msgid "Errors and Exceptions" msgstr "錯誤和例外" #: ../../tutorial/errors.rst:7 msgid "" "Until now error messages haven't been more than mentioned, but if you have " "tried out the examples you have probably seen some. There are (at least) " "two distinguishable kinds of errors: *syntax errors* and *exceptions*." msgstr "" "到目前為止還沒有提到錯誤訊息,但如果你嘗試運行範例,你可能會發現一些錯誤訊" "息。常見的(至少)兩種不同的錯誤類別為:\\ *語法錯誤 (syntax error)* 和\\ *例" "外 (exception)*\\ 。" #: ../../tutorial/errors.rst:15 msgid "Syntax Errors" msgstr "語法錯誤 (Syntax Error)" #: ../../tutorial/errors.rst:17 msgid "" "Syntax errors, also known as parsing errors, are perhaps the most common " "kind of complaint you get while you are still learning Python::" msgstr "" "語法錯誤又稱剖析錯誤 (parsing error),它或許是學習 Python 的過程最常聽見的抱" "怨:\n" "\n" "::" #: ../../tutorial/errors.rst:26 msgid "" "The parser repeats the offending line and displays a little 'arrow' pointing " "at the earliest point in the line where the error was detected. The error " "is caused by (or at least detected at) the token *preceding* the arrow: in " "the example, the error is detected at the function :func:`print`, since a " "colon (``':'``) is missing before it. File name and line number are printed " "so you know where to look in case the input came from a script." msgstr "" "剖析器 (parser) 會重複犯錯的那一行,並用一個小「箭頭」指向該行檢測到的第一個" "錯誤點。錯誤是由箭頭\\ *之前*\\ 的標記 (token) 導致的(或至少是在這裡檢測到" "的):此例中,錯誤是在 :func:`print` 函式中被檢測到,因為在它前面少了一個冒號" "(\\ ``':'``)。檔案名稱和行號會被印出來,所以如果訊息是來自腳本時,就可以知" "道去哪裡找問題。" #: ../../tutorial/errors.rst:37 msgid "Exceptions" msgstr "例外 (Exception)" #: ../../tutorial/errors.rst:39 msgid "" "Even if a statement or expression is syntactically correct, it may cause an " "error when an attempt is made to execute it. Errors detected during " "execution are called *exceptions* and are not unconditionally fatal: you " "will soon learn how to handle them in Python programs. Most exceptions are " "not handled by programs, however, and result in error messages as shown " "here::" msgstr "" "即使一段陳述式或運算式使用了正確的語法,嘗試執行時仍可能導致錯誤。執行時檢測" "到的錯誤稱為\\ *例外*\\ ,例外不一定都很嚴重:你很快就能學會在 Python 程式中" "如何處理它們。不過大多數的例外不會被程式處理,並且會顯示如下的錯誤訊息:\n" "\n" "::" #: ../../tutorial/errors.rst:58 msgid "" "The last line of the error message indicates what happened. Exceptions come " "in different types, and the type is printed as part of the message: the " "types in the example are :exc:`ZeroDivisionError`, :exc:`NameError` and :exc:" "`TypeError`. The string printed as the exception type is the name of the " "built-in exception that occurred. This is true for all built-in exceptions, " "but need not be true for user-defined exceptions (although it is a useful " "convention). Standard exception names are built-in identifiers (not reserved " "keywords)." msgstr "" "錯誤訊息的最後一行指示發生了什麼事。例外有不同的類型,而類型名稱會作為訊息的" "一部份被印出。範例中的例外類型為:\\ :exc:`ZeroDivisionError`\\ 、\\ :exc:" "`NameError` 和 :exc:`TypeError`\\ 。作為例外類型被印出的字串,就是發生的內建" "例外 (built-in exception) 的名稱。所有的內建例外都是如此運作,但對於使用者自" "定的例外則不一定需要遵守(雖然這是一個有用的慣例)。標準例外名稱是內建的識別" "字 (identifier),不是保留關鍵字 (reserved keyword)。" #: ../../tutorial/errors.rst:66 msgid "" "The rest of the line provides detail based on the type of exception and what " "caused it." msgstr "此行其餘部分,根據例外的類型及導致例外的原因,說明例外的細節。" #: ../../tutorial/errors.rst:69 msgid "" "The preceding part of the error message shows the context where the " "exception occurred, in the form of a stack traceback. In general it contains " "a stack traceback listing source lines; however, it will not display lines " "read from standard input." msgstr "" "錯誤訊息的開頭,用堆疊回溯 (stack traceback) 的形式顯示發生例外的語境。一般來" "說,它含有一個列出源程式碼行 (source line) 的堆疊回溯;但它不會顯示從標準輸入" "中讀取的程式碼。" #: ../../tutorial/errors.rst:74 msgid "" ":ref:`bltin-exceptions` lists the built-in exceptions and their meanings." msgstr ":ref:`bltin-exceptions`\\ 章節列出內建的例外及它們的意義。" #: ../../tutorial/errors.rst:80 msgid "Handling Exceptions" msgstr "處理例外" #: ../../tutorial/errors.rst:82 msgid "" "It is possible to write programs that handle selected exceptions. Look at " "the following example, which asks the user for input until a valid integer " "has been entered, but allows the user to interrupt the program (using :kbd:" "`Control-C` or whatever the operating system supports); note that a user-" "generated interruption is signalled by raising the :exc:`KeyboardInterrupt` " "exception. ::" msgstr "" "編寫程式處理選定的例外是可行的。以下範例會要求使用者輸入內容,直到有效的整數" "被輸入為止,但它允許使用者中斷程式(使用 :kbd:`Control-C` 或作業系統支援的指" "令);請注意,由使用者產生的程式中斷會引發 :exc:`KeyboardInterrupt` 例外信" "號。\n" "\n" "::" #: ../../tutorial/errors.rst:96 msgid "The :keyword:`try` statement works as follows." msgstr ":keyword:`try` 陳述式運作方式如下。" #: ../../tutorial/errors.rst:98 msgid "" "First, the *try clause* (the statement(s) between the :keyword:`try` and :" "keyword:`except` keywords) is executed." msgstr "" "首先,執行 *try 子句*\\ (\\ :keyword:`try` 和 :keyword:`except` 關鍵字之間的" "陳述式)。" #: ../../tutorial/errors.rst:101 msgid "" "If no exception occurs, the *except clause* is skipped and execution of the :" "keyword:`try` statement is finished." msgstr "" "如果沒有發生例外,則 *except 子句*\\ 會被跳過,\\ :keyword:`try` 陳述式執行完" "畢。" #: ../../tutorial/errors.rst:104 msgid "" "If an exception occurs during execution of the :keyword:`try` clause, the " "rest of the clause is skipped. Then, if its type matches the exception " "named after the :keyword:`except` keyword, the *except clause* is executed, " "and then execution continues after the try/except block." msgstr "" "如果執行 :keyword:`try` 子句時發生了例外,則該子句中剩下的部分會被跳過。如果" "例外的類型與 :keyword:`except` 關鍵字後面的例外名稱相符,則 *except 子句*\\ " "被執行,然後,繼續執行 try/except 區塊之後的程式碼。" #: ../../tutorial/errors.rst:109 msgid "" "If an exception occurs which does not match the exception named in the " "*except clause*, it is passed on to outer :keyword:`try` statements; if no " "handler is found, it is an *unhandled exception* and execution stops with a " "message as shown above." msgstr "" "如果發生的例外未符合 *except 子句*\\ 中的例外名稱,則將其傳遞到外層的 :" "keyword:`try` 陳述式;如果仍無法找到處理者,則它是一個\\ *未處理例外 " "(unhandled exception)*\\ ,執行將停止,並顯示如上所示的訊息。" #: ../../tutorial/errors.rst:114 msgid "" "A :keyword:`try` statement may have more than one *except clause*, to " "specify handlers for different exceptions. At most one handler will be " "executed. Handlers only handle exceptions that occur in the corresponding " "*try clause*, not in other handlers of the same :keyword:`!try` statement. " "An *except clause* may name multiple exceptions as a parenthesized tuple, " "for example::" msgstr "" ":keyword:`try` 陳述式可以有不只一個 *except 子句*\\ ,為不同的例外指定處理" "者,而最多只有一個處理者會被執行。處理者只處理對應的 try 子句中發生的例外,而" "不會處理同一 :keyword:`!try` 陳述式裡其他處理者內的例外。一個 *except 子句" "*\\ 可以用一組括號內的 tuple 列舉多個例外,例如:\n" "\n" "::" #: ../../tutorial/errors.rst:123 msgid "" "A class in an :keyword:`except` clause is compatible with an exception if it " "is the same class or a base class thereof (but not the other way around --- " "an *except clause* listing a derived class is not compatible with a base " "class). For example, the following code will print B, C, D in that order::" msgstr "" "一個在 :keyword:`except` 子句中的 class(類別)和一個例外是可相容的,只要它與" "例外是同一個 class 或是為其 base class(基底類別);反之則無法成立——列出 " "derived class (衍生類別)的 *except 子句*\\ 並不能與 base class 相容。例如," "以下程式碼會依序印出 B、C、D:\n" "\n" "::" #: ../../tutorial/errors.rst:147 msgid "" "Note that if the *except clauses* were reversed (with ``except B`` first), " "it would have printed B, B, B --- the first matching *except clause* is " "triggered." msgstr "" "請注意,如果 *except 子句*\\ 的順序被反轉(把 ``except B`` 放到第一個),則會" "印出 B、B、B ­­——第一個符合的 *except 子句*\\ 會被觸發。" #: ../../tutorial/errors.rst:150 msgid "" "When an exception occurs, it may have associated values, also known as the " "exception's *arguments*. The presence and types of the arguments depend on " "the exception type." msgstr "" "當例外發生時,它可能有相關聯的值,也就是例外的\\ *引數*\\ 。引數的存在與否及" "它的類型,是取決於例外的類型。" #: ../../tutorial/errors.rst:154 msgid "" "The *except clause* may specify a variable after the exception name. The " "variable is bound to the exception instance which typically has an ``args`` " "attribute that stores the arguments. For convenience, builtin exception " "types define :meth:`__str__` to print all the arguments without explicitly " "accessing ``.args``. ::" msgstr "" "*except 子句*\\ 可以在例外名稱後面指定一個變數。這個變數被綁定到一個例外實例 " "(instance),其引數通常儲存在 ``args`` 屬性中。為了方便,內建例外型別定義了 :" "meth:`__str__` 以印出所有引數而不需顯式地取用 ``.args``\\ :\n" "\n" "::" #: ../../tutorial/errors.rst:177 msgid "" "The exception's :meth:`__str__` output is printed as the last part " "('detail') of the message for unhandled exceptions." msgstr "" "例外的 :meth:`__str__` 輸出會被印在未處理例外訊息的最後一部分(「細節」)。" #: ../../tutorial/errors.rst:180 msgid "" ":exc:`BaseException` is the common base class of all exceptions. One of its " "subclasses, :exc:`Exception`, is the base class of all the non-fatal " "exceptions. Exceptions which are not subclasses of :exc:`Exception` are not " "typically handled, because they are used to indicate that the program should " "terminate. They include :exc:`SystemExit` which is raised by :meth:`sys." "exit` and :exc:`KeyboardInterrupt` which is raised when a user wishes to " "interrupt the program." msgstr "" ":exc:`BaseException` 是由全部的例外所共用的 base class。它的 subclass(子類" "別)之一,:exc:`Exception`,則是所有非嚴重例外 (non-fatal exception) 的 base " "class。有些例外不是 :exc:`Exception` 的 subclass,而它們通常不會被處理,因為" "它們是用來指示程式應該終止。這些例外包括了由 :meth:`sys.exit` 所引發的 :exc:" "`SystemExit`,以及當使用者想要中斷程式時所引發的 :exc:`KeyboardInterrupt`。" #: ../../tutorial/errors.rst:188 msgid "" ":exc:`Exception` can be used as a wildcard that catches (almost) everything. " "However, it is good practice to be as specific as possible with the types of " "exceptions that we intend to handle, and to allow any unexpected exceptions " "to propagate on." msgstr "" ":exc:`Exception` 可以用作通配符 (wildcard) 來捕獲(幾乎)所有的例外。然而,比" "較好的做法是盡可能具體地說明我們打算處理的例外類型,並容許任何非預期例外的傳" "遞 (propagate)。" #: ../../tutorial/errors.rst:193 msgid "" "The most common pattern for handling :exc:`Exception` is to print or log the " "exception and then re-raise it (allowing a caller to handle the exception as " "well)::" msgstr "" "處理 :exc:`Exception` 的最常見模式,是先將該例外印出或記錄,然後再重新引發它" "(也允許一個呼叫函式 (caller) 來處理該例外):\n" "\n" "::" #: ../../tutorial/errors.rst:211 msgid "" "The :keyword:`try` ... :keyword:`except` statement has an optional *else " "clause*, which, when present, must follow all *except clauses*. It is " "useful for code that must be executed if the *try clause* does not raise an " "exception. For example::" msgstr "" ":keyword:`try` ... :keyword:`except` 陳述式有一個選擇性的 *else 子句*\\ ,使" "用時,該子句必須放在所有 *except 子句*\\ 之後。如果一段程式碼必須被執行,但 " "*try 子句*\\ 又沒有引發例外時,這個子句很有用。例如:\n" "\n" "::" #: ../../tutorial/errors.rst:225 msgid "" "The use of the :keyword:`!else` clause is better than adding additional code " "to the :keyword:`try` clause because it avoids accidentally catching an " "exception that wasn't raised by the code being protected by the :keyword:`!" "try` ... :keyword:`!except` statement." msgstr "" "使用 :keyword:`!else` 子句比向 :keyword:`try` 子句添加額外的程式碼要好,因為" "這可以避免意外地捕獲不是由 :keyword:`!try` ... :keyword:`!except` 陳述式保護" "的程式碼所引發的例外。" #: ../../tutorial/errors.rst:230 msgid "" "Exception handlers do not handle only exceptions that occur immediately in " "the *try clause*, but also those that occur inside functions that are called " "(even indirectly) in the *try clause*. For example::" msgstr "" "例外的處理者不僅處理 *try 子句*\\ 內立即發生的例外,還處理 *try 子句*\\ 內" "(即使是間接地)呼叫的函式內部發生的例外。例如:\n" "\n" "::" #: ../../tutorial/errors.rst:248 msgid "Raising Exceptions" msgstr "引發例外" #: ../../tutorial/errors.rst:250 msgid "" "The :keyword:`raise` statement allows the programmer to force a specified " "exception to occur. For example::" msgstr "" ":keyword:`raise` 陳述式可讓程式設計師強制引發指定的例外。例如:\n" "\n" "::" #: ../../tutorial/errors.rst:258 msgid "" "The sole argument to :keyword:`raise` indicates the exception to be raised. " "This must be either an exception instance or an exception class (a class " "that derives from :class:`BaseException`, such as :exc:`Exception` or one of " "its subclasses). If an exception class is passed, it will be implicitly " "instantiated by calling its constructor with no arguments::" msgstr "" ":keyword:`raise` 唯一的引數就是要引發的例外。該引數必須是一個例外實例或例外 " "class(衍生自 :class:`BaseException` 的 class,例如 :class:`Exception` 與它" "的 subclass)。如果一個例外 class 被傳遞,它會不含引數地呼叫它的建構函式 " "(constructor) ,使它被自動建立實例 (implicitly instantiated):\n" "\n" "::" #: ../../tutorial/errors.rst:266 msgid "" "If you need to determine whether an exception was raised but don't intend to " "handle it, a simpler form of the :keyword:`raise` statement allows you to re-" "raise the exception::" msgstr "" "如果你只想判斷是否引發了例外,但並不打算處理它,則可以使用簡單的 :keyword:" "`raise` 陳述式來重新引發該例外:\n" "\n" "::" #: ../../tutorial/errors.rst:285 msgid "Exception Chaining" msgstr "例外鏈接 (Exception Chaining)" #: ../../tutorial/errors.rst:287 msgid "" "If an unhandled exception occurs inside an :keyword:`except` section, it " "will have the exception being handled attached to it and included in the " "error message::" msgstr "" "如果在 :keyword:`except` 段落內部發生了一個未處理的例外,則它會讓這個將要被處" "理的例外附加在後,並將其包含在錯誤訊息中:\n" "\n" "::" #: ../../tutorial/errors.rst:306 msgid "" "To indicate that an exception is a direct consequence of another, the :" "keyword:`raise` statement allows an optional :keyword:`from` clause::" msgstr "" "為了表明一個例外是另一個例外直接造成的結果,:keyword:`raise` 陳述式容許一個選" "擇性的 :keyword:`from` 子句:\n" "\n" "::" #: ../../tutorial/errors.rst:312 msgid "This can be useful when you are transforming exceptions. For example::" msgstr "" "要變換例外時,這種方式很有用。例如:\n" "\n" "::" #: ../../tutorial/errors.rst:333 msgid "" "It also allows disabling automatic exception chaining using the ``from " "None`` idiom::" msgstr "" "它也容許使用慣用語 ``from None`` 來停用自動例外鏈接:\n" "\n" "::" #: ../../tutorial/errors.rst:345 msgid "" "For more information about chaining mechanics, see :ref:`bltin-exceptions`." msgstr "更多關於鏈接機制的資訊,詳見\\ :ref:`bltin-exceptions`\\ 。" #: ../../tutorial/errors.rst:351 msgid "User-defined Exceptions" msgstr "使用者自定的例外" #: ../../tutorial/errors.rst:353 msgid "" "Programs may name their own exceptions by creating a new exception class " "(see :ref:`tut-classes` for more about Python classes). Exceptions should " "typically be derived from the :exc:`Exception` class, either directly or " "indirectly." msgstr "" "程式可以通過建立新的例外 class 來命名自己的例外(深入了解 Python class,詳見" "\\ :ref:`tut-classes`\\ )。不論是直接還是間接地,例外通常應該從 :exc:" "`Exception` class 衍生出來。" #: ../../tutorial/errors.rst:357 msgid "" "Exception classes can be defined which do anything any other class can do, " "but are usually kept simple, often only offering a number of attributes that " "allow information about the error to be extracted by handlers for the " "exception." msgstr "" "例外 class 可被定義來做任何其他 class 能夠做的事,但通常會讓它維持簡單,只提" "供一些屬性,讓關於錯誤的資訊可被例外的處理者抽取出來。" #: ../../tutorial/errors.rst:361 msgid "" "Most exceptions are defined with names that end in \"Error\", similar to the " "naming of the standard exceptions." msgstr "大多數的例外定義,都會以「Error」作為名稱結尾,類似於標準例外的命名。" #: ../../tutorial/errors.rst:364 msgid "" "Many standard modules define their own exceptions to report errors that may " "occur in functions they define." msgstr "許多標準模組會定義它們自己的例外,以報告在其定義的函式中發生的錯誤。" #: ../../tutorial/errors.rst:371 msgid "Defining Clean-up Actions" msgstr "定義清理動作" #: ../../tutorial/errors.rst:373 msgid "" "The :keyword:`try` statement has another optional clause which is intended " "to define clean-up actions that must be executed under all circumstances. " "For example::" msgstr "" ":keyword:`try` 陳述式有另一個選擇性子句,用於定義在所有情況下都必須被執行的清" "理動作。例如:\n" "\n" "::" #: ../../tutorial/errors.rst:387 msgid "" "If a :keyword:`finally` clause is present, the :keyword:`!finally` clause " "will execute as the last task before the :keyword:`try` statement completes. " "The :keyword:`!finally` clause runs whether or not the :keyword:`!try` " "statement produces an exception. The following points discuss more complex " "cases when an exception occurs:" msgstr "" "如果 :keyword:`finally` 子句存在,則 :keyword:`!finally` 子句會是 :keyword:" "`try` 陳述式結束前執行的最後一項任務。不論 :keyword:`!try` 陳述式是否產生例" "外,都會執行 :keyword:`!finally` 子句。以下幾點將探討例外發生時,比較複雜的情" "況:" #: ../../tutorial/errors.rst:393 msgid "" "If an exception occurs during execution of the :keyword:`!try` clause, the " "exception may be handled by an :keyword:`except` clause. If the exception is " "not handled by an :keyword:`!except` clause, the exception is re-raised " "after the :keyword:`!finally` clause has been executed." msgstr "" "若一個例外發生於 :keyword:`!try` 子句的執行過程,則該例外會被某個 :keyword:" "`except` 子句處理。如果該例外沒有被 :keyword:`!except` 子句處理,它會在 :" "keyword:`!finally` 子句執行後被重新引發。" #: ../../tutorial/errors.rst:399 msgid "" "An exception could occur during execution of an :keyword:`!except` or :" "keyword:`!else` clause. Again, the exception is re-raised after the :keyword:" "`!finally` clause has been executed." msgstr "" "一個例外可能發生於 :keyword:`!except` 或 :keyword:`!else` 子句的執行過程。同" "樣地,該例外會在 :keyword:`!finally` 子句執行後被重新引發。" #: ../../tutorial/errors.rst:403 msgid "" "If the :keyword:`!finally` clause executes a :keyword:`break`, :keyword:" "`continue` or :keyword:`return` statement, exceptions are not re-raised." msgstr "" "如果 :keyword:`!finally` 子句執行 :keyword:`break`\\ 、\\ :keyword:" "`continue` 或 :keyword:`return` 陳述式,則例外不會被重新引發。" #: ../../tutorial/errors.rst:407 msgid "" "If the :keyword:`!try` statement reaches a :keyword:`break`, :keyword:" "`continue` or :keyword:`return` statement, the :keyword:`!finally` clause " "will execute just prior to the :keyword:`!break`, :keyword:`!continue` or :" "keyword:`!return` statement's execution." msgstr "" "如果 :keyword:`!try` 陳述式遇到 :keyword:`break`\\ 、\\ :keyword:`continue` " "或 :keyword:`return` 陳述式,則 :keyword:`!finally` 子句會在執行 :keyword:`!" "break`\\ 、\\ :keyword:`!continue` 或 :keyword:`!return` 陳述式之前先執行。" #: ../../tutorial/errors.rst:413 msgid "" "If a :keyword:`!finally` clause includes a :keyword:`!return` statement, the " "returned value will be the one from the :keyword:`!finally` clause's :" "keyword:`!return` statement, not the value from the :keyword:`!try` " "clause's :keyword:`!return` statement." msgstr "" "如果 :keyword:`!finally` 子句中包含 :keyword:`!return` 陳述式,則回傳值會是來" "自 :keyword:`!finally` 子句的 :keyword:`!return` 陳述式的回傳值,而不是來自 :" "keyword:`!try` 子句的 :keyword:`!return` 陳述式的回傳值。" #: ../../tutorial/errors.rst:419 msgid "For example::" msgstr "" "例如:\n" "\n" "::" #: ../../tutorial/errors.rst:430 msgid "A more complicated example::" msgstr "" "另一個比較複雜的範例:\n" "\n" "::" #: ../../tutorial/errors.rst:455 msgid "" "As you can see, the :keyword:`finally` clause is executed in any event. " "The :exc:`TypeError` raised by dividing two strings is not handled by the :" "keyword:`except` clause and therefore re-raised after the :keyword:`!" "finally` clause has been executed." msgstr "" "如你所見,\\ :keyword:`finally` 子句在任何情況下都會被執行。兩個字串相除所引" "發的 :exc:`TypeError` 沒有被 :keyword:`except` 子句處理,因此會在 :keyword:`!" "finally` 子句執行後被重新引發。" #: ../../tutorial/errors.rst:460 msgid "" "In real world applications, the :keyword:`finally` clause is useful for " "releasing external resources (such as files or network connections), " "regardless of whether the use of the resource was successful." msgstr "" "在真實應用程式中,\\ :keyword:`finally` 子句對於釋放外部資源(例如檔案或網路" "連線)很有用,無論該資源的使用是否成功。" #: ../../tutorial/errors.rst:468 msgid "Predefined Clean-up Actions" msgstr "預定義的清理動作" #: ../../tutorial/errors.rst:470 msgid "" "Some objects define standard clean-up actions to be undertaken when the " "object is no longer needed, regardless of whether or not the operation using " "the object succeeded or failed. Look at the following example, which tries " "to open a file and print its contents to the screen. ::" msgstr "" "某些物件定義了在物件不再被需要時的標準清理動作,無論使用該物件的作業是成功或" "失敗。請看以下範例,它嘗試開啟一個檔案,並印出檔案內容至螢幕。\n" "\n" "::" #: ../../tutorial/errors.rst:478 msgid "" "The problem with this code is that it leaves the file open for an " "indeterminate amount of time after this part of the code has finished " "executing. This is not an issue in simple scripts, but can be a problem for " "larger applications. The :keyword:`with` statement allows objects like files " "to be used in a way that ensures they are always cleaned up promptly and " "correctly. ::" msgstr "" "這段程式碼的問題在於,執行完該程式碼後,它讓檔案在一段不確定的時間內處於開啟" "狀態。在簡單腳本中這不是問題,但對於較大的應用程式來說可能會是個問題。\\ :" "keyword:`with` 陳述式讓物件(例如檔案)在被使用時,能保證它們總是及時、正確地" "被清理。\n" "\n" "::" #: ../../tutorial/errors.rst:488 msgid "" "After the statement is executed, the file *f* is always closed, even if a " "problem was encountered while processing the lines. Objects which, like " "files, provide predefined clean-up actions will indicate this in their " "documentation." msgstr "" "陳述式執行完畢後,就算是在處理內容時遇到問題,檔案 *f* 總是會被關閉。和檔案一" "樣,提供預定義清理動作的物件會在說明文件中表明這一點。" #: ../../tutorial/errors.rst:496 msgid "Raising and Handling Multiple Unrelated Exceptions" msgstr "引發及處理多個無關的例外" #: ../../tutorial/errors.rst:498 msgid "" "There are situations where it is necessary to report several exceptions that " "have occurred. This is often the case in concurrency frameworks, when " "several tasks may have failed in parallel, but there are also other use " "cases where it is desirable to continue execution and collect multiple " "errors rather than raise the first exception." msgstr "" "在某些情況下,必須回報已經發生的多個例外。在並行框架 (concurrency framework) " "中經常會出現這種情況,當平行的 (parallel) 某些任務可能已經失效,但還有其他用" "例 (use case) 希望能繼續執行並收集多個例外,而不是只有引發第一個例外時。" #: ../../tutorial/errors.rst:504 msgid "" "The builtin :exc:`ExceptionGroup` wraps a list of exception instances so " "that they can be raised together. It is an exception itself, so it can be " "caught like any other exception. ::" msgstr "" "內建的 :exc:`ExceptionGroup` 會包裝一個例外實例 (exception instance) 的 list" "(串列),使得它們可以一起被引發。由於它本身就是一個例外,因此它也可以像任何" "其他例外一樣被捕獲。\n" "\n" "::" #: ../../tutorial/errors.rst:530 msgid "" "By using ``except*`` instead of ``except``, we can selectively handle only " "the exceptions in the group that match a certain type. In the following " "example, which shows a nested exception group, each ``except*`` clause " "extracts from the group exceptions of a certain type while letting all other " "exceptions propagate to other clauses and eventually to be reraised. ::" msgstr "" "若使用 ``except*`` 代替 ``except``,我們可以選擇性地只處理該群組中與特定類型" "匹配的例外。在以下範例中,展示了一個巢狀的例外群組 (exception group),每個 " "``except*`` 子句分別從該群組中提取一個特定類型的例外,同時讓所有其他的例外都" "傳遞到其他子句,最後再被重新引發。\n" "\n" "::" #: ../../tutorial/errors.rst:564 msgid "" "Note that the exceptions nested in an exception group must be instances, not " "types. This is because in practice the exceptions would typically be ones " "that have already been raised and caught by the program, along the following " "pattern::" msgstr "" "請注意,被巢套在例外群組中的例外必須是實例,而不是類型。這是因為在實務上,這" "些例外通常是已經被程式引發並捕獲的例外,類似以下的模式:\n" "\n" "::" #: ../../tutorial/errors.rst:584 msgid "Enriching Exceptions with Notes" msgstr "用註解使例外更詳細" #: ../../tutorial/errors.rst:586 msgid "" "When an exception is created in order to be raised, it is usually " "initialized with information that describes the error that has occurred. " "There are cases where it is useful to add information after the exception " "was caught. For this purpose, exceptions have a method ``add_note(note)`` " "that accepts a string and adds it to the exception's notes list. The " "standard traceback rendering includes all notes, in the order they were " "added, after the exception. ::" msgstr "" "當一個例外是為了被引發而建立時,它通常會伴隨著一些資訊被初始化,這些資訊描述" "了當下發生的錯誤。在某些情況,在例外被捕獲之後添加資訊會很有用。為此,例外具" "有一個 ``add_note(note)`` method(方法),它可以接受一個字串並將其添加到例外" "的註解清單中。標準的回溯呈現會在例外之後列出所有的註解,並按照其被添加的順序" "來排列。\n" "\n" "::" #: ../../tutorial/errors.rst:607 msgid "" "For example, when collecting exceptions into an exception group, we may want " "to add context information for the individual errors. In the following each " "exception in the group has a note indicating when this error has occurred. ::" msgstr "" "例如,在將例外收集到例外群組中時,我們可能希望為各個錯誤添加一些上下文的資" "訊。在以下範例中,群組中的每個例外都有一條註解,指示此錯誤是在何時發生。\n" "\n" "::" #~ msgid "" #~ "All exceptions inherit from :exc:`BaseException`, and so it can be used " #~ "to serve as a wildcard. Use this with extreme caution, since it is easy " #~ "to mask a real programming error in this way! It can also be used to " #~ "print an error message and then re-raise the exception (allowing a caller " #~ "to handle the exception as well)::" #~ msgstr "" #~ "所有例外繼承自 :exc:`BaseException`\\ ,以統一處理所有其他例外,但使用上要" #~ "極其小心,因為這種方式容易遮蔽真正的程式設計錯誤!它也可用於印出錯誤訊息," #~ "然後重新引發例外(也讓呼叫者可以處理該例外):\n" #~ "\n" #~ "::" #~ msgid "" #~ "Alternatively the last except clause may omit the exception name(s), " #~ "however the exception value must then be retrieved from ``sys.exc_info()" #~ "[1]``." #~ msgstr "" #~ "或者讓最後一個 except 子句可以省略例外名稱,但之後例外的值必須是從 ``sys." #~ "exc_info()[1]`` 得到的。" #~ msgid "" #~ "The :keyword:`raise` statement allows an optional :keyword:`from` " #~ "which enables chaining exceptions. For example::" #~ msgstr "" #~ ":keyword:`raise` 陳述式容許一個選擇性的 :keyword:`from`\\ ,它透過" #~ "被引發例外中的 ``__cause__`` 屬性的設定,來啟用例外鏈接。例如:\n" #~ "\n" #~ "::" #~ msgid "" #~ "Exception chaining happens automatically when an exception is raised " #~ "inside an :keyword:`except` or :keyword:`finally` section. This can be " #~ "disabled by using ``from None`` idiom:" #~ msgstr "" #~ "當例外是在一個 :keyword:`except` 或 :keyword:`finally` 段落的內部被引發" #~ "時,例外鏈接會自動發生。要使其停止作用,可以使用慣用語 ``from None``:"