Why does break in a for loop not work when finally is involved

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Why does break in a for loop not work when finally is involved

Nils Andresen
Hi,
The following code:
print(" - Start");
int[] num = new int[] {1,2,3,4,5,6,7,8,9,10};
for(int i : num)
{
    print("--> "+ i);
    try
    {
        Print("    . try");
        if(i == 2)
            break;
    }
    catch (java.lang.Exception e)
    {}
    finally
    {
        print("    . finally");
    }
}
print(" - End");

does not break as expected but runs all 10 iterations. If I remove the
finally block everything works as expected. Can someone tell me why
this is?

Best,
Nils

P.S.
I posted this question at [1] but it seems there are not so many
beanshell users at stackoverflow...

1: http://stackoverflow.com/questions/11113948/why-does-break-in-a-for-loop-not-work-when-finally-is-involved-in-beanshell

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Why does break in a for loop not work when finally is involved

Nils Andresen
Hi,
to have this "complete".
This seems to be a bug in beanshell. It's fixed in beanshell2, though.

Best,
Nils

2012/6/21 Nils Andresen <[hidden email]>:

> Hi,
> The following code:
> print(" - Start");
> int[] num = new int[] {1,2,3,4,5,6,7,8,9,10};
> for(int i : num)
> {
>     print("--> "+ i);
>     try
>     {
>         Print("    . try");
>         if(i == 2)
>             break;
>     }
>     catch (java.lang.Exception e)
>     {}
>     finally
>     {
>         print("    . finally");
>     }
> }
> print(" - End");
>
> does not break as expected but runs all 10 iterations. If I remove the
> finally block everything works as expected. Can someone tell me why
> this is?
>
> Best,
> Nils
>
> P.S.
> I posted this question at [1] but it seems there are not so many
> beanshell users at stackoverflow...
>
> 1: http://stackoverflow.com/questions/11113948/why-does-break-in-a-for-loop-not-work-when-finally-is-involved-in-beanshell

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users