Try statements are new to me, so is a try except statement equivalent to execute or execute fail code?
This is how I see it in bash:
cmd || otherCmd
try-except statement is a code block that allows your program to take alternative actions in case an error occurs. Python will first attempt to execute the code in the try statement. If no exception occurs, the except statement is skipped and the execution of the try statement is finished. If any exception occurs, the except statement will trigger. This way your program handle exceptions instead stopping.
Related
module xyz;
always
begin : b1
$display("I am executing"); // statement 1
disable b1;
$display("I am still executing"); // statement 2
end
endmodule
I am unable to understand how disable statement actually behaves in the above code.
I was expecting that the statement 1 will execute only once and then the always block (b1) will be disabled forever.
But actually statement 1 is being executed infinitely (until the process is killed) and statement 2 is being skipped.
I am executing
I am executing
I am executing
.
.
.
I have tried all simulators of EDA Playground.
A disable name statement is essentially a jump to the end of the named block. It does not terminate any processes (except nested fork/join blocks)
The always construct creates a permanent process that executes the procedural statement that follows it. Once that statement completes, it executes it over again indefinitely. In your example, that procedural statement is a begin/end block
in the following example code:
with open(filename,"r") as file:
return file.read()
does the return statement cause any potential instability?
does the "with" operator still provide its safeguards under these circumstances?
since the return happens, and nothing is executed after the return statement, does the with operator still perform its closing operations that ensure the stability of the file operations and close file pointers?
EDIT:
This seems to answer the question:
In Python, if I return inside a "with" block, will the file still close?
This question already has answers here:
What is the intended use of the optional "else" clause of the "try" statement in Python?
(22 answers)
Closed 1 year ago.
I am learning Python. I have a fundamental doubt about the "try--except--else--finally" statement sequence.
As I understand if code in 'try' goes through then code in 'else' will also get executed. If that is the case, what is the need for 'else' block? Whatever code is under 'else' block can be in 'try' block itself.
Appreciate clarifications
Else block gets executed when try block raises no error.
Except block gets executed when try raises an error.
finally block gets executed regardless of whether try raises an error or not
Docs about it can be found here
The process goes on this way:
try section is executed normally
if there's error (exception) raised, it goes to the except block
if there's no error raised in try section, then else block will be executed
then comes finally block. finally is independent of exceptions. It is executed always
I am running test cases using pytest; although I would like to collect some logs and zip everything, only if there is a failure
I did write the function that collect logs and zip it, although I can't find a way to actually trigger that for each failed case. I did implement a solution using the #classmethod decorators for setUpClass and tearDownClass; but this happen only after all the tests have been ran, and not when a specific test fail.
In each test I run a simple assertFalse(a < b, "the test did fail". I would like to trigger the function that does the log collection only when that condition is actually triggered.
Seems that there is no direct way to do it in the assert statement, or I was not able to find a way for it.
I just used try-catch and in the try I raise an exception,and in the catch code I call the function. Not sure if this is the best way, but it works in my case
try:
if (a < b):
raise ValueError ("wrong output, collecting logs")
except ValueError:
# call function to parse logs
self.parseoutputonerror(a, b, "/tmp/out.zip")
With exceptions being so central to idiomatic Python, is there a clean way to execute a particular code block if an expression evaluates to True or the evaluation of the expression raises an exception? By clean, I mean an easy-to-read, Pythonic, and not repeating the code block?
For example, instead of:
try:
if some_function(data) is None:
report_error('Something happened')
except SomeException:
report_error('Something happened') # repeated code
can this be cleanly rewritten so that report_error() isn't written twice?
(Similar question: How can I execute same code for a condition in try block without repeating code in except clause but this is for a specific case where the exception can be avoided by a simple test within the if statement.)
Yes, this can be done relatively cleanly, although whether it can be considered good style is an open question.
def test(expression, exception_list, on_exception):
try:
return expression()
except exception_list:
return on_exception
if test(lambda: some_function(data), SomeException, None) is None:
report_error('Something happened')
This comes from an idea in the rejected PEP 463.
lambda to the Rescue presents the same idea.