try...catch
blocks are generally encourages to be used less, and this doesn’t depend on the language you use.
The main reason for this is the cost of catch
blocks. Also another reason is that, when you wrap many statements with a single try...catch
block, in catch
block you can’t be sure what was exactly the main problem.
It’s better to use techniques like input validation or if...else
blocks to reduce the probability of an exception (error) to happen. For example, when you want to work with a number which is taken from user, instead of using try...catch
, you can use:
if (isNaN(numberVariable))
{
alert('you should enter a valid number');
}