No matter how great we are at programming sometimes our code has errors.
Errors are a natural part of programming , what matters is how effectively we handle and fix them.
In this blog we will look into these methods that gracefully handles errors.
Topics to Cover
- What errors are in JavaScript
- Using try and catch blocks
- The finally block
- Throwing custom errors
- Why error handling matters
Errors
Errors are the problems or mistakes in a programming that cause it to behave incorrectly and stop running.
There are types of error which which make your code to behave incorrectly
Common types
Syntax error :- Wrong syntax
example :- Missing brackets , typoReference error :- Using a variable that is not defined
example :- console.log(x) when x doesn't existType error :- using a value in wrong way
example :- calling something that is not a function
Above are some types of errors I commonly encounter in my daily coding.
There are ways that i handle these are gracefully lets see one by one.
Using try and catch blocks
This is primary mechanism for error handling in javascript
Here is the basic syntax for try..catch
try {
// some code
} catch (err) {
// this will run only if the code in the try block errors-out
}
It works like :
- First the code in
try{}block is executed. - If there is no error the
catch(err){}block is ignored. - If an error occurs, Then the
try{}execution is stopped and thecatch(err){}block will start executing.
The err variable will contain an error object with details about what happened.
Lets take some examples
Here is the example we will misspell a variable name
let hello = 2;
try {
console.log(hilleo + 4);
} catch (e) {
console.log('Error -', e);
}
Output of the above code :
Error - ReferenceError: hilleo is not defined
The finally block
You can also use a finally{} block to ensure that some code runs no matter if the try block executes or not.
let hello = 2;
try {
console.log(hilleo + 4);
} catch (e) {
console.log('Error -', e);
}finally{
console.log("Like this blog")
}
Output of the above code :
Error - ReferenceError: hilleo is not defined
Like this blog
Throwing Custom Errors
There are some problem oftentimes that we cant able to detect where the error is coming from or its hard to understand to catch pure JavaScript errors.
For this we can make use of throw statement to throw our own errors in a try block so that can can be caught and we can deal with it gracefully.
try{
const {email , password} = req.body
if(!email || !password){
throw new Error("Fields are missing")
}
console.log("Email fetch")
}catch(err){
console.log("error" , err)
}
Output of the above code :
Mising feilds:
Fields are missing
Valid data:
Email fetch
Why error handling matters
Following are the reason why error handling matters the most :
- Prevents crashes – avoids breaking the app when something goes wrong
- Better user experience – shows meaningful messages instead of blank screens
- Easier debugging – helps identify and fix issues faster
- Improves reliability – app continues to work even with unexpected inputs
- Security – prevents leaking sensitive information through raw errors
- Maintainability – cleaner code with predictable behavior
Thanks for reading ! if enjoyed this blog , you can read more on this 👇
Top comments (0)