The Basics of Try/Catch

RMAG news

Have you run into a problem when coding and just can’t find where that error is coming
from? This error could be anything from a typo, missing line of code, or undefined data. Thanks to that error it could also stop your console.log from running, making it harder to see what some variables equal to, or where the error is coming from. Luckily, there is a code statement you can use to help avoid having that problem Try/Catch. Similar to the .then and .catch methods used in promise, it runs a line of code, and if there is an error the catch method will catch it and then run the code in the Catch method. Here is a blog I wrote for promises that use .then and .catch methods in its code https://dev.to/gagecantrelle/the-basic-of-promises-amf.

Try/Catch history

During the early days of JavaScript when it was still somewhat new to the world, the developers were adding error-checking methods to it. By 1999 for javaScript version 1.4 the Try/Catch statement was added. It Was commonly used in code to check if it was running properly or not. Then at a later point in time, the Finally statement was added to the Try/Catch method. It works similarly to promises and is set similarly to an if statement. Making it easy to understand how it works and set up.

** How it works**

like I said earlier Try/Catch is set up like an if statement. Though it can’t be extended like an
else if statement. You can only have one Try, Catch, and Finally statement. For the Try code block to work you must have a Catch or Finally code block.

try{
//the Try method does not take in any parameter or conditions.
//when a line of code in a Try method runs into an error, it will
//stop running all the code in the Try method and send an error argument //to the Catch method.
let str = 1 + 2 + ”;
//the console should log a string of 3
console.log(str);
}catch (error){
//the Catch method will take an error argument that is related to the
//error that happens in the Try method. Though the Catch method can
//take in an argument it can be used without one to
//example
console.log(error);
console.log(‘error on line 11’);
};
//result will be ‘3’

The Finally statment can be added to the end of the Catch method or can replace the Catch
method, It will always run if there is an error or not.

try {
let str = 1 + 2 + ”;
console.log(str);
} catch (error){
console.log(error);
console.log(‘error on line 6’);
} finally {
//will run after Catch even if there is an error or not
console.log(‘hey your Try block on line 1 has run’)
}
//result will be ‘3’ and ‘hey your Try method on line 1 has run’

Try/Catch examples

Import { highOrderFunction } from ‘./HOF.js;
Import { data } from ‘./examples.js’

let result = null;

Try{
result = highOrderFunction(data).map()
}catch{
console.log(‘Error: the high order function on line 7 in HOFTEST.js, data given is not the correct data type’)
result = [‘default’];
}

Let’s say you have a high-order function and running it with another high-order function, there are times when you might run into an error. Normally JavaScript will atomically throw an error in the console, which sometimes doesn’t explain what caused the error or when it at. We can use the catch method to console log where the error coming from. If we know what our code is doing and how it can break, we can also log the type of error we might be having

function getData (url){
return fetch(url)
.then((data)=>{
let changeData = parse.JSON(data)

try{

return changeData.images[3]

}catch(error){
console.log(error)
}

}
}

Another good use for try/catch is in promises. Let’s say you used the fetch method to send a request and you receive the data from the server. Although there are no errors here, There is a chance that there is something wrong with the data or requested not. You can use try/catch to check if the data requested is fulfilled or rejected because of an error.

Leave a Reply

Your email address will not be published. Required fields are marked *