Javascript try/catch blocks are pretty standard and straight forward.

try{
  // try some stuff and fail
  throw new Error('failsauce');
}
catch(error){
  console.log(error);
  // Error {
  //   name    : ErrorName,
  //   message : 'error message'
  // }
}

Note that the catch block passes in an error argument which you can then access the error/exception's properties to learn more about what exactly when down.

You're exceptional

You can throw your own custom Objects and then access their properties in the catch block.

try{
  throw {
    name    : 'FooError',
    message : 'foo message'
  };
}
catch(error){
  console.log(error);
  // Error {
  //   name    : FooError,
  //   message : 'foo error'
  // }
}

or another way

function FooError(){
  this.name = 'FooError';
  this.message = 'foo message';
}
try{
  throw new FooError();
}
catch(error){
  console.log(error);
  // Error {
  //   name    : FooError,
  //   message : 'foo error'
  // }
}

All together now

function UserException(message){
  this.message = message;
  this.name = 'UserException';
}

function returnUser(userName){
  var users = {
    'cgcardona' : {
      fName : 'carlos',
      lName : 'cardona'
    }, 
    'sol' : {
      fName : 'soljah',
      lName : 'cardona'
    }, 
    'natalia': {
      fName : 'natalie',
      lName : 'cardona'
    }
  };
  if(users[userName] !== undefined)
    return users[userName];
  else {
    throw new UserException('User ' + userName + ' is not registered');
  }
}

try{
  var user = returnUser('natalia');
  console.log(user);
  // Object {fName: "natalie", lName: "cardona"} 

  var user = returnUser('foobar');
}
catch(e){
  console.log(e);
  // UserException {message: "User foobar is not registered", name: "UserException"} 
}

More info


Thanks for reading! Follow me on Twitter and/or G+ for more.

See something wrong or got something to add/change? Fork this git repo and send me a pull request

If you particularly enjoy my work, I appreciate donations given with Gitcoin.co



Published

25 February 2013

Tags