How javascript works ... catch statement

I am trying to check in browsers if some input field works or not. I am trying to use a try ... catch statement that I have never used before. I know the form:

try {
//some code
} catch (){
//some error code
};

What exactly is supposed to be placed in parentheses after discharge? When I try to use a statement, it runs everything through a catch statement, regardless of whether this is an error. What am I doing wrong?

+5
source share
5 answers

Refer to the " try...catch" manual in MDN .

, try/catch ( "" throw). try/catch:

try {
    // Code
} catch (varName) {              // Optional
    // If exception thrown in try block,
    // execute this block
} finally {                      // Optional
    // Execute this block after
    // try or after catch clause
    // (i.e. this is *always* called)
}

varName catch. , ( , , String, Error object).

+9

catch try /, try -block. catch .

:

try {
 // code that may fail with error/exception
} catch (e) { // e represents the exception/error object
 // react
}

:

try {
  var x = parseInt("xxx");
  if(isNaN(x)){
    throw new Error("Not a number");
  }
} catch (e) { // e represents the exception/error object
 alert(e);
}

try {
 // some code
 if(!condition){
   throw new Error("Something went wrong!");
 }
} catch (e) { // e represents the exception/error object
 alert(e);
}
+3

try {...} - , . catch() {...} - , , javascript , try {...}

catch {...} javascript try {...}. , , :

try {
 // do something 
} catch (err) {
  alert(err);
}
+1

ECMAScript,

try {
    // Code
} catch (varName) {  // optional if 'finally' block is present.
  if (condition) {   // eg. (varName instanceof URIError)
    // Condition (Type) specific error handling
  }
  else {
    // Generic error handling
  }
} finally {          // Optional if 'catch' block is present.
    // Execute this block after
    // try or after catch clause
    // (i.e. this is *always* called)
}
+1

, , try { }. , , catch() { }. catch() , , . finally { } , .

0

All Articles