Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
59
rated 0 times [  65] [ 6]  / answers: 1 / hits: 24282  / 9 Years ago, thu, august 13, 2015, 12:00:00

I'd like to silently break a try - catch in the try block if a condition applies. ( Without throwing an unneccessary exception )



foo = function(){

var bar = Math.random() > .5;

try{

if( bar ) // Break this try, even though there is no exception here.

// This code should not execute if !!bar

alert( bar );

}
catch( e ){}

// Code that executes if !!bar

alert( true );

}

foo();


However, return is not an option, since the function is supposed to continue executing afterwards.



UPDATE



I'd like to still keep up the opportunity to use the finally block.


More From » operators

 Answers
15

You can label a block and break from it using the break label syntax



as per your edit, finally is still executed



foo = function(){
var bar = Math.random() > .5;
omgalabel: try {
if( bar ) break omgalabel;
console.log( bar );
// code
}
catch( e ){
// This code should not execute if !!bar
}
finally {
// Code that executes no matter what
console.log( true );
}
}

[#65423] Tuesday, August 11, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leilanichasel

Total Points: 224
Total Questions: 112
Total Answers: 94

Location: Angola
Member since Wed, Apr 13, 2022
2 Years ago
;