My understanding of Python's with
statement is as follows:
with
statement = with
+ expression + as
+ target + :
+ suit
__enter__
returns a value to target
__exit__
method is invokedI know exceptions can be handled in step2 and step3, my question is that if an exception is thrown during the step1 when expression is executed, can I get a context manager?
If not does that mean that the with
statement just ensures the suit to be executed and close properly?
Like with open("file") as f
, if the file does not exist what will happen?
When an exception is raised, no further statements in the current block of code are executed.
Python Exceptions. When a Python program meets an error, it stops the execution of the rest of the program. An error in Python might be either an error in the syntax of an expression or a Python exception.
The effect of a raise statement is to either divert execution in a matching except suite, or to stop the program because no matching except suite was found to handle the exception. The exception object created by raise can contain a message string that provides a meaningful error message.
Since the try block raises an error, the except block will be executed.
The with
statement only manages exceptions in step 3. If an exception is raised in step 1 (executing expression) or in step 2 (executing the context manager __enter__
method), you do not have a (valid and working) context manager to hand the exception to.
So if the file does not exist, an exception is raised in step 1 and cannot be handled by a context manager, because that context manager was never created.
If that is a problem, you can always execute the expression part separately:
try:
context_manager = expression
except SomeSpecificException:
# do something about the exception
else:
with context_manager as target:
# execute the suite
If the exception is raised in __enter__
(step 2) the context hasn’t yet been entered and so __exit__
will not be called. Your only option to handle an exception at that step is to put the whole with
statement inside a try...except
block.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With