Was ist raise, finally usw ?

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Septias
User
Beiträge: 80
Registriert: Freitag 24. Juni 2016, 19:15

Hallo,

Ich habe überhaupt keine Ahnung was das ist, also entschuldigt bitte meine Fehler ;D

Ja, wie schon im Titel ist meine Frage: Was ist finally, raise (wenn die überhaupt einen Obergriff haben...) usw. ? Ich habe jetzt schon nen bisschen in den Python-docs herumgesucht, bin aber nur auf ne PEP gestoßen, die finally erklährt hat... D: . Und auch mit help (Im IDLE) lässt sich nichts herausfinden. Gibt es eine Seite wo man alle nachlesen kann oder nen Namen für diese "Funktionen" damit man nachlesen kann, was die verschiedenen Sachen machen und wie man sie einsetzt (z.B was danach stehen muss).

Thx ^^
Für alle meine Codebeispiele gilt: Äußert bitte jegliche Art von Verbesserungsvorschlägen. Ich versuche immer meinen Stil zu verbessern und wenn man mir einfach sagt, was ich falsch machen, ist es um einiges einfacher, als wenn ich es mühselig selber herausfinden muss :-)
BlackJack

@Septias: Oberbegriff wäre ”Ausnahmebehandlung” und das wird beispielsweise im Tutorial in der Python-Dokumentation beschrieben: Errors and Exceptions. Und natürlich in der Referenz bei den Abschnitten zu den einzelnen Schlüsselwörtern/Anweisungen.

Edit: Also ich bekomme mit ´`help('finally')`` in IDLE etwas:
[codebox=pycon file=Unbenannt.txt]>>> help('finally')
The ``try`` statement
*********************

The ``try`` statement specifies exception handlers and/or cleanup code
for a group of statements:

try_stmt ::= try1_stmt | try2_stmt
try1_stmt ::= "try" ":" suite
("except" [expression [("as" | ",") target]] ":" suite)+
["else" ":" suite]
["finally" ":" suite]
try2_stmt ::= "try" ":" suite
"finally" ":" suite

Changed in version 2.5: In previous versions of Python,
``try``...``except``...``finally`` did not work. ``try``...``except``
had to be nested in ``try``...``finally``.

The ``except`` clause(s) specify one or more exception handlers. When
no exception occurs in the ``try`` clause, no exception handler is
executed. When an exception occurs in the ``try`` suite, a search for
an exception handler is started. This search inspects the except
clauses in turn until one is found that matches the exception. An
expression-less except clause, if present, must be last; it matches
any exception. For an except clause with an expression, that
expression is evaluated, and the clause matches the exception if the
resulting object is "compatible" with the exception. An object is
compatible with an exception if it is the class or a base class of the
exception object, a tuple containing an item compatible with the
exception, or, in the (deprecated) case of string exceptions, is the
raised string itself (note that the object identities must match, i.e.
it must be the same string object, not just a string with the same
value).

If no except clause matches the exception, the search for an exception
handler continues in the surrounding code and on the invocation stack.
[1]

If the evaluation of an expression in the header of an except clause
raises an exception, the original search for a handler is canceled and
a search starts for the new exception in the surrounding code and on
the call stack (it is treated as if the entire ``try`` statement
raised the exception).

When a matching except clause is found, the exception is assigned to
the target specified in that except clause, if present, and the except
clause's suite is executed. All except clauses must have an
executable block. When the end of this block is reached, execution
continues normally after the entire try statement. (This means that
if two nested handlers exist for the same exception, and the exception
occurs in the try clause of the inner handler, the outer handler will
not handle the exception.)

Before an except clause's suite is executed, details about the
exception are assigned to three variables in the ``sys`` module:
``sys.exc_type`` receives the object identifying the exception;
``sys.exc_value`` receives the exception's parameter;
``sys.exc_traceback`` receives a traceback object (see section *The
standard type hierarchy*) identifying the point in the program where
the exception occurred. These details are also available through the
``sys.exc_info()`` function, which returns a tuple ``(exc_type,
exc_value, exc_traceback)``. Use of the corresponding variables is
deprecated in favor of this function, since their use is unsafe in a
threaded program. As of Python 1.5, the variables are restored to
their previous values (before the call) when returning from a function
that handled an exception.

The optional ``else`` clause is executed if and when control flows off
the end of the ``try`` clause. [2] Exceptions in the ``else`` clause
are not handled by the preceding ``except`` clauses.

If ``finally`` is present, it specifies a 'cleanup' handler. The
``try`` clause is executed, including any ``except`` and ``else``
clauses. If an exception occurs in any of the clauses and is not
handled, the exception is temporarily saved. The ``finally`` clause is
executed. If there is a saved exception, it is re-raised at the end
of the ``finally`` clause. If the ``finally`` clause raises another
exception or executes a ``return`` or ``break`` statement, the saved
exception is lost. The exception information is not available to the
program during execution of the ``finally`` clause.

When a ``return``, ``break`` or ``continue`` statement is executed in
the ``try`` suite of a ``try``...``finally`` statement, the
``finally`` clause is also executed 'on the way out.' A ``continue``
statement is illegal in the ``finally`` clause. (The reason is a
problem with the current implementation --- this restriction may be
lifted in the future).

Additional information on exceptions can be found in section
*Exceptions*, and information on using the ``raise`` statement to
generate exceptions may be found in section *The raise statement*.

Related help topics: EXCEPTIONS[/code]
Und wenn man dem letzten Hinweis folgt:
[codebox=pycon file=Unbenannt.txt]>>> help('EXCEPTIONS')
Exceptions
**********

Exceptions are a means of breaking out of the normal flow of control
of a code block in order to handle errors or other exceptional
conditions. An exception is *raised* at the point where the error is
detected; it may be *handled* by the surrounding code block or by any
code block that directly or indirectly invoked the code block where
the error occurred.

The Python interpreter raises an exception when it detects a run-time
error (such as division by zero). A Python program can also
explicitly raise an exception with the ``raise`` statement. Exception
handlers are specified with the ``try`` ... ``except`` statement. The
``finally`` clause of such a statement can be used to specify cleanup
code which does not handle the exception, but is executed whether an
exception occurred or not in the preceding code.

Python uses the "termination" model of error handling: an exception
handler can find out what happened and continue execution at an outer
level, but it cannot repair the cause of the error and retry the
failing operation (except by re-entering the offending piece of code
from the top).

When an exception is not handled at all, the interpreter terminates
execution of the program, or returns to its interactive main loop. In
either case, it prints a stack backtrace, except when the exception is
``SystemExit``.

Exceptions are identified by class instances. The ``except`` clause
is selected depending on the class of the instance: it must reference
the class of the instance or a base class thereof. The instance can
be received by the handler and can carry additional information about
the exceptional condition.

Exceptions can also be identified by strings, in which case the
``except`` clause is selected by object identity. An arbitrary value
can be raised along with the identifying string which can be passed to
the handler.

Note: Messages to exceptions are not part of the Python API. Their
contents may change from one version of Python to the next without
warning and should not be relied on by code which will run under
multiple versions of the interpreter.

See also the description of the ``try`` statement in section *The try
statement* and ``raise`` statement in section *The raise statement*.

-[ Footnotes ]-

[1] This limitation occurs because the code that is executed by these
operations is not available at the time the module is compiled.

Related help topics: try, except, finally, raise[/code]
Septias
User
Beiträge: 80
Registriert: Freitag 24. Juni 2016, 19:15

ja guuuuut... ;D. Hatte keine Ahnung und habe es nur so probiert : help (finally).
Für alle meine Codebeispiele gilt: Äußert bitte jegliche Art von Verbesserungsvorschlägen. Ich versuche immer meinen Stil zu verbessern und wenn man mir einfach sagt, was ich falsch machen, ist es um einiges einfacher, als wenn ich es mühselig selber herausfinden muss :-)
Antworten