JAVASCRIPT JOURNEY - DAY 8

#teamtanayejschallenge

CHAPTER 8 :ERROR HANDLING

js1.png

Error handling is an important part of programs.

When our programs encounter values that are unexpected we have to properly handle them.

Remember: Throwing exceptions is better because they let us know that an error exists and that we have to handle it.

Also Exceptions are cleaner since we don’t have to check all the codes that may be returned.

Usually, a script “dies” (immediately stops) in case of an error, printing it to console.

But there’s a syntax construct: try/catch that allows us to “catch” errors so the script can, instead of dying, do something more reasonable.

  try {

  alert('Start program'); 

  alert('End of program');   

} catch(err) {

  alert(''If error , catch ignored"); 

}

Always wrap the code we want to run in a try block. Now we can just catch the error instead of checking all the error codes that may be returned.

When we do this ,

It creates its own scope for block-scoped variables so anything declared with let or const can only be referenced in the try block.

Variables declared with var are hoisted so that they can be referenced outside the block. We won’t get an error even if they’re referenced outside the block.

Summary:

When we handle errors, throwing exceptions is better than returning error codes since they let us use the try...catch block to handle errors.

USE-STRICT???

Strict mode makes it easier to write "secure" JavaScript.

Strict mode changes previously accepted "bad syntax" into real errors.

Strict mode helps out in a couple ways:

1.. It catches some common coding bloopers, throwing exceptions.

  1. It prevents, or throws errors, when relatively "unsafe" actions are taken .

But Exceptions are much better than it as It limits you a lot and debugging got hard with it.

Happy learning:-)