python exception not raised if finally returns value [duplicate]












4
















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









share|improve this question













marked as duplicate by VPfB, jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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


















4
















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









share|improve this question













marked as duplicate by VPfB, jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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
















4












4








4









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









share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 16:34









ezdazuzenaezdazuzena

3,36932453




3,36932453




marked as duplicate by VPfB, jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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





















  • 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














1 Answer
1






active

oldest

votes


















0














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






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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






    share|improve this answer




























      0














      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






      share|improve this answer


























        0












        0








        0







        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






        share|improve this answer













        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 16:38









        Sam MasonSam Mason

        3,34711331




        3,34711331

















            Popular posts from this blog

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]