Does javascript have built-in error codes? Is it possible to catch an internal error? can I continue?

ok, I feel stupid asking this question and forgive my bad research methods, but ...

using example

  if (obj.attr1.attr2.attr3 == 'constant') return;
   else if (condition2) ...

if obj.attr1 is undefined, the javascript engine throws an error.

  • What is the error that occurs? Is it universally defined?

  • can globally capture this error?

  • if it is captured, is it possible to execute the next line condition2 ?

to clarify: the error occurs due to an attempt to get the undefined attribute. is there any way to know that this is a mistake? is it in some kind of table of standard javascript error messages?

and secondly, capturing the error upstream, is it possible for the program to continuously flow?

+4
source share
2 answers

It is possible to capture this error using the try / catch block:

 try{ if( obj.attr1.attr2.attr3 == 'constant' ) { alert("test"); } } catch(e) { alert(e.Message); } 

The exception gives the following:

 description "'obj' is undefined" String message "'obj' is undefined" String name "TypeError" String number -2146823279 Number 
+2
source

Usually this is solved gracefully, not blindly assuming that there is something.

 if( obj.attr1 && obj.attr1.attr2 && obj.attr1.attr2.attr3 == 'constant' ) 

In addition, you can write a try / catch statement that catches this exception, but using structured exception handling to control the normal flow of a program is discouraged and should be avoided.

0
source

All Articles