Elias Prado
Elias Prado

Reputation: 1817

How to write an error handling block using try/catch and finally?

I am trying to understand the result of a challenge in my course and I am stuck to pass, as the result is not matching with what they are expecting. The challenge tasks are:

  1. Write a try/catch block that tries to console log myVar, which should be deliberately undefined, so as to generate an error Catch the error.
  2. In the catch block, store the entire error in a variable, myError
  3. In the catch block, store the error's name in a variable, myErrorName
  4. In the catch block, store the error's message in a variable, myErrorMessage Write a finally block that checks to see if myError exists.
  5. If so, define a variable, result, which has the following value: There was an error (myErrorName: myErrorMessage)
  6. If not, define the same variable, result, but assign it the value "No errors occurred!'

The approach I have is as follow:

try{
  myVar();
}
catch(err){
  var myError = err;
  var myErrorName = err.name;
  var myErrorMessage = err.message;
}
 finally{
  if(myError == err){
    var result = "There was an error (myErrorName:myErrorMessage)";
 }else{
    console.log("No error ocurred");
 }
}

Upvotes: 1

Views: 879

Answers (2)

Gaurav
Gaurav

Reputation: 543

Check this It will work

try{
  console.log(myVar);
}
catch(err){
  var myError = err;
  var myErrorName = err.name;
  var myErrorMessage = err.message;
}
 finally{
  if(myError){
    var result = "There was an error ("+ myErrorName+": "+myErrorMessage+")";
 }else{
   result = "No error ocurred";
 }
}

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074088

There are two primary problems with that code:

  1. You have a typo: var myErro = err; is missing the r on the end of myErro.

  2. You're trying to use err in the finally block, but it's only in scope in the catch block.

...and then a few more that don't match what it seems like the assignment is telling you to do

  1. You're not doing #1, you're doing something else which coincidentally also tries to use myVar

  2. You don't have any code implementing #6.

  3. Your code assigning result when there's an error is putting myErrorName and myErrorMessage literally in the string, rather than using the values of those variables.

  4. Your message for when no errors have occurred is not the same as the message they told you to use, it has slight differences (including a typo). Programming is at least partially about attention to detail. :-)

You don't need if (myError == err), just if (myError) will do:

try{
  myVar();
}
catch(err){
  var myError = err;
  var myErrorName = err.name;
  var myErrorMessage = err.message;
}
 finally{
  if(myError){
    var result = "There was an error (myErrorName:myErrorMessage)";
    console.log(result);
 }else{
    console.log("No error ocurred");
 }
}

(I added a console.log so we'd see the error case.)

I've only addressed #1 and #2 in the above, the rest are left as an exercise for you to complete. :-)


I should note that I wouldn't write it that way. Declaring a variable in the catch block that you use in the finally block does work, with var (because var is not block-scoped and is hoisted), but it's misleading to people trying to maintain the code. If you're going to use a variable in both of those blocks, move the declaration outside the blocks for clarity.

Upvotes: 7

Related Questions