Quick View on Error Handling

Quick View on Error Handling

Β·

2 min read

Hello learners πŸ‘‹

We have to accept one thing here Error is part of programming and fixing and learning it's to way of growing!!!!!

What is error handling in Javascript πŸ€”πŸ› οΈ ?

how it came into picture Let's understand via code πŸ‘‡

let error = "error Handling";
console.log(error + error2);
// what it will give me, it will give me refference error and stop execution here
// It mean it will not be able to execute next line code.
console.log(
    "Operation"
// Big operation not performed here 😞😞
);

😦 so because of this complication error handling came into picture got it Yes

1tu3qm.jpg

What is error handling (Exception Handling)

    Error : Error it mean the program gives
    Handling: Handling it mean to take care of that program

Defination:

  • Javascript provide an error handling mechanism to catch run time errors using try -catch-finally block.
  • Error handling handles the runtime errors in the JavaScript. try is the keyword creates the try block. Here the code that can cause exception is under the wrap under this block. The catch is the keyword creates the catch block.
    //  try to relate or differentiate this example with above example 
    //  which we performed πŸ‘†πŸ‘†
    try{
      let errors = "error Handling"
      console.log(errors + "" + error2);
    }
    catch(e){
    console.log(e.message);
    console.log(e.name);
    console.log(e);
    }finally{
      console.log("operation Successful");
    // Yeah it workπŸ‘πŸ‘
    }
    

download.jpg

But one thing I have a doubt what is this try-catch-finally-block πŸ€”πŸ€”πŸ€·β€β™‚οΈπŸ€·β€β™‚οΈ I don't understand let's understand via theoretical way see this πŸ‘‡πŸ‘‡

try{
    // we have to write code here in those way which will throw error
}catch(){
    // As soon as the try pass error here it will catch here 
}finally{
    // Either try throw error or not Catch work or not it will run at any cost
}
  • Error handling handles the runtime errors in the JavaScript
  • try is the keyword creates the try block. Here the code that can cause exception is under the wrap under this block.
  • Thecatchis the keyword that creates the catch block. Additionally, we use it to catch the exception thrown from the try block and execute the code.
  • Finally, the block runs a piece of code always whether there is an exception or not.
  • The throw keyword can create customized exceptions, which can be thrown based on certain conditions.
Β