python exception not raised if finally returns value [duplicate]
This question already has an answer here:
Weird Try-Except-Else-Finally behavior with Return statements
2 answers
Can anyone explain why the following example does not raise the Exception
?
def foo():
try:
0/0
except Exception:
print('in except')
raise
finally:
print('in finally')
return 'bar'
my_var = foo()
print(my_var)
This just returns:
in except
in finally
bar
Where as the same code without the return 'bar'
statement throws the exception:
in except
in finally
Traceback (most recent call last):
File "test.py", line 10, in <module>
my_var = foo()
File "test.py", line 3, in foo
0/0
ZeroDivisionError: division by zero
python exception exception-handling try-except
marked as duplicate by VPfB, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 21:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Weird Try-Except-Else-Finally behavior with Return statements
2 answers
Can anyone explain why the following example does not raise the Exception
?
def foo():
try:
0/0
except Exception:
print('in except')
raise
finally:
print('in finally')
return 'bar'
my_var = foo()
print(my_var)
This just returns:
in except
in finally
bar
Where as the same code without the return 'bar'
statement throws the exception:
in except
in finally
Traceback (most recent call last):
File "test.py", line 10, in <module>
my_var = foo()
File "test.py", line 3, in foo
0/0
ZeroDivisionError: division by zero
python exception exception-handling try-except
marked as duplicate by VPfB, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 21:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
you can catch the exception, assign it to a previously defined None variable and then reraise it in finally: Bit trickier to get working in python 3 than 2, i’ll post it later. useful to close resources but still throw the exception
– JL Peyret
Nov 22 '18 at 21:27
add a comment |
This question already has an answer here:
Weird Try-Except-Else-Finally behavior with Return statements
2 answers
Can anyone explain why the following example does not raise the Exception
?
def foo():
try:
0/0
except Exception:
print('in except')
raise
finally:
print('in finally')
return 'bar'
my_var = foo()
print(my_var)
This just returns:
in except
in finally
bar
Where as the same code without the return 'bar'
statement throws the exception:
in except
in finally
Traceback (most recent call last):
File "test.py", line 10, in <module>
my_var = foo()
File "test.py", line 3, in foo
0/0
ZeroDivisionError: division by zero
python exception exception-handling try-except
This question already has an answer here:
Weird Try-Except-Else-Finally behavior with Return statements
2 answers
Can anyone explain why the following example does not raise the Exception
?
def foo():
try:
0/0
except Exception:
print('in except')
raise
finally:
print('in finally')
return 'bar'
my_var = foo()
print(my_var)
This just returns:
in except
in finally
bar
Where as the same code without the return 'bar'
statement throws the exception:
in except
in finally
Traceback (most recent call last):
File "test.py", line 10, in <module>
my_var = foo()
File "test.py", line 3, in foo
0/0
ZeroDivisionError: division by zero
This question already has an answer here:
Weird Try-Except-Else-Finally behavior with Return statements
2 answers
python exception exception-handling try-except
python exception exception-handling try-except
asked Nov 22 '18 at 16:34
ezdazuzenaezdazuzena
3,36932453
3,36932453
marked as duplicate by VPfB, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 21:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by VPfB, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 21:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
you can catch the exception, assign it to a previously defined None variable and then reraise it in finally: Bit trickier to get working in python 3 than 2, i’ll post it later. useful to close resources but still throw the exception
– JL Peyret
Nov 22 '18 at 21:27
add a comment |
you can catch the exception, assign it to a previously defined None variable and then reraise it in finally: Bit trickier to get working in python 3 than 2, i’ll post it later. useful to close resources but still throw the exception
– JL Peyret
Nov 22 '18 at 21:27
you can catch the exception, assign it to a previously defined None variable and then reraise it in finally: Bit trickier to get working in python 3 than 2, i’ll post it later. useful to close resources but still throw the exception
– JL Peyret
Nov 22 '18 at 21:27
you can catch the exception, assign it to a previously defined None variable and then reraise it in finally: Bit trickier to get working in python 3 than 2, i’ll post it later. useful to close resources but still throw the exception
– JL Peyret
Nov 22 '18 at 21:27
add a comment |
1 Answer
1
active
oldest
votes
see https://stackoverflow.com/a/19805813/1358308 for more detail, but in brief
the finally
block should always be executed, Python therefore has to ignore the raise
statement as that would violate semantics
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
see https://stackoverflow.com/a/19805813/1358308 for more detail, but in brief
the finally
block should always be executed, Python therefore has to ignore the raise
statement as that would violate semantics
add a comment |
see https://stackoverflow.com/a/19805813/1358308 for more detail, but in brief
the finally
block should always be executed, Python therefore has to ignore the raise
statement as that would violate semantics
add a comment |
see https://stackoverflow.com/a/19805813/1358308 for more detail, but in brief
the finally
block should always be executed, Python therefore has to ignore the raise
statement as that would violate semantics
see https://stackoverflow.com/a/19805813/1358308 for more detail, but in brief
the finally
block should always be executed, Python therefore has to ignore the raise
statement as that would violate semantics
answered Nov 22 '18 at 16:38
Sam MasonSam Mason
3,34711331
3,34711331
add a comment |
add a comment |
you can catch the exception, assign it to a previously defined None variable and then reraise it in finally: Bit trickier to get working in python 3 than 2, i’ll post it later. useful to close resources but still throw the exception
– JL Peyret
Nov 22 '18 at 21:27