Exceptions for control flow I just wrote the following javascript (jQeury loaded):

var valid, ui;

try {
    $.each(this._cache, function() {
        $.each(this, function() {
            $.each(this, function() {
                if (this.label.toLowerCase() === valueLowerCase) {
                    valid = true;
                    ui = {item: $.extend({}, this) };
                    throw 'Control flow throw, please rethrow if accidently caught';
                }
            });
        });
    });
} catch(e) {}


My coworker suggested I rewrite this as follows:

var valid, ui;

$.each(this._cache, function() {
    $.each(this, function() {
        $.each(this, function() {
            if (this.label.toLowerCase() === valueLowerCase) {
                valid = true;
                ui = {item: $.extend({}, this) };
            }
            return !valid;
        });
        return !valid;
    });
    return !valid;
});


I think this is a valid case of using exceptions as control flow. Which way is the correct way to do this?
As Jef Vanzella mentioned, exceptions are for unexpected bad things, not for jumping out of control flow. Hence the second option is better, but it is still substandard code.

Instead of using this, you should have a meaningful parameter name in your function declaration.

Also, you could simply return the falsey !ui instead of !valid.

Finally, not many people know that returning false stops the iteration, that deserves a comment.

Now imagine that your data structure was Cache -&gt; Orders -&gt; Shipments -&gt; Items then you could write:

var ui;

//Returning false to each() will stop the iteration
$.each(this._cache, function( order ) {
    $.each(order, function( shipment ) {
        $.each(shipment, function( item) {
            if (item.label.toLowerCase() === valueLowerCase) {
                ui = {item: $.extend({}, this) };
            }
            return !ui ;
        });
        return !ui ;
    });
    return !ui ;
});