Error Handling, "try...catch"

Error Handling, "try...catch"

When we write code, it's common to encounter errors or unexpected behaviors that can cause your program to crash or stop working as intended. One way to handle these issues is by using a "try" and "catch" statement.

The "try" statement lets you test a block of code for errors. If an error occurs in the "try" block, it jumps to the "catch" block, where you can handle the error and prevent it from crashing your program.

Here's an example of how to use "try" and "catch" in JavaScript:

try {
  // code that may throw an error
  let x = y + z; // y and z are not defined
  console.log(x)
} catch (error) {
  // code to handle the error
  console.log("An error occurred: " + error);
}

In this example, we are trying to add two variables "y" and "z" that are not defined. This will throw an error and cause the program to crash. However, we've enclosed this code in a "try" statement, which means that any errors will be caught and handled by the "catch" block.

When an error occurs in the "try" block, the program jumps to the "catch" block, where you can handle the error. In this case, we are simply printing an error message to the console using the "console.log" function.

Another useful feature of the "catch" block is that you can access information about the error using the "error" parameter. This parameter is an object that contains information about the error, such as its message, name, and stack trace. You can use this information to debug your code and fix the error.

Here's an example of how to use the "error" parameter in the "catch" block:

try {
  // code that may throw an error
  let x = y + z; // y and z are not defined
} catch (error) {
  // code to handle the error
  console.log("An error occurred: " + error.message); // prints "y is not defined"
  console.log("Error name: " + error.name); // prints "ReferenceError"
  console.log("Stack trace: " + error.stack); // prints the stack trace of the error
}

In this example, we're accessing the "message", "name", and "stack" properties of the "error" object to get information about the error. This can be useful when debugging your code and trying to figure out what went wrong.

How does it prevent our program from crashing...

let's take an example to understand how it will prevent our program from crashing :

const convertToRs = dollar => {
   if(typeof dollar === 'number'){
     return dollar * 60;
   } else {
     throw Error('Amount need to be in number')
   }
}
console.log(convertToRs('five')) // Uncaught Error: Amount need to be in number
console.log("I will not run if program crashes") // This console will not run

When we run this program, we get an error with "Amount need to be in number" but the next console will not run, here our program will crash, if we use "try" and "catch" we can easily prevent our program from crashing.

const convertToRs = dollar => {
   if(typeof dollar === 'number'){
     return dollar * 60;
   } else {
     throw Error('Amount need to be in number')
   }
}
try {
  console.log(convertToRs('five'))
}
 catch(error) {
 console.log(error) // Error: Amount need to be in number
}
console.log("I will run even program crashes") // I will run even program crashes

When we run this code with "try" and "catch", it will give us an error too but it will not block the next console to run and this is how we can prevent our program from crashing.