Should I always port code in Try Catch blocks for Javascript?

I have ignored exception handling for too long, mainly because I have never seen it used in the snippets I see on SO after 2 years of writing javascript, and I didn't even know that javascript handling has exception handling. Other languages ​​that are often found in exception handling - exception handling less important for javascript? What are typical scenarios, you should always use exception handling in javascript, not?

Is this rule also applicable to javascript?

Why shouldn't I wrap each block in a try - catch?

+5
source share
3 answers

javascript , .

. , , :

var element;

if (document && document.getElementById) {
  element = document.getElementById('foo');
}

if (element) {
  // use element
}    

, isHostMethod.

try..catch , . (, XMLHttpRequest try..catch, IE , - , ).

script , . ( SO) , , . , , ( ). , , - script, script . , , , .

, .

+3

C .

++ . Java , (: Integer.parseInt: , - String, ).

JavaScript . . jQuery , , 0 .

, , , . .

+1

An exception is a big problem when it comes to performance. But this makes error detection in a very simple way.

If someones tools are used that throw an exception, you must run them through the block try catch.

Try to minimize throwing exception when javascript coding

0
source

All Articles