python exception not raised if finally returns value [duplicate]











up vote
4
down vote

favorite













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 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 at 21:27

















up vote
4
down vote

favorite













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 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 at 21:27















up vote
4
down vote

favorite









up vote
4
down vote

favorite












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 at 16:34









ezdazuzena

3,16022350




3,16022350




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 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 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 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 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 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 at 21:27














1 Answer
1






active

oldest

votes

















up vote
0
down vote













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








    up vote
    0
    down vote













    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

























      up vote
      0
      down vote













      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























        up vote
        0
        down vote










        up vote
        0
        down vote









        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 at 16:38









        Sam Mason

        2,39511226




        2,39511226















            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo