I have a piece of code which looks something like this:
try {
if( !request.body || !request.body.device ) {
throw new FooError( "missing 'device'" );
}
if( !request.body.device.type ) {
throw new BarError( "missing 'device.type'" );
}
if( request.body.device.type != "ios" ) {
throw new BazError( "unexpected 'device.type'" );
}
} catch( error ) {
// handle error and communicate message to the user
}
WebStorm highlights the 3 throw
terms and gives the following explanation:
'throw' of exception caught locally
This inspection reports any instances of JavaScript throw statements whose exceptions are always caught by containing try statements. Using throw statements as a "goto" to change the local flow of control is likely to be confusing.
I don't understand that reasoning. Why would this likely be confusing and what would be the less confusing alternative?