JavaScript try catch finally: Handle Errors Cleanly
Wrap risky code in try, catch, and finally to show clear error messages instead of silent failures. See how throw and finally work together in practice.


With the JavaScript try, catch, throw, and finally statements, we can create clearer error messages based on what happens in a code block. During development, we sometimes run into different errors in our code. These errors can occur in situations we can predict and sometimes in situations we can’t. That’s why using custom error codes is important both for debugging and for informing the user.
- Try lets us run our code inside a code block, and depending on what happens there, the catch, throw, and finally statements are used.
- Catch lets us catch the error.
- Throw lets us create custom errors.
- Finally lets you run code after try and catch, regardless of the result.
The structure is as follows;
try {
//the code block to run
}
catch {
//the error is caught here, and whatever should happen when an error occurs is done in this block.
}
finally {
// This is the code block that should run whether or not an error occurs.
}
try {
test("Welcome guest!");
}
catch(err) {
console.log(err.message);
}
// as output, it will print an error message like test is not defined to the console.
As you can see, when we ran the code above, we got an error message and caught it inside the catch block. After catching it with catch, we printed it to the console.
Let’s create error messages with JavaScript THROWS.
I mentioned that with throw you can create customized error messages, meaning error messages you define however you like. The error messages you create with throw can be of type STRING, BOOLEAN, NUMBER, or OBJECT.
In JavaScript throws, the error types that come back will be Runtime or Syntax errors.
Runtime Error: This error type can be caused by calling the wrong variable or using the wrong function while the code block is running.
Syntax Error: If you make a typo while writing the code, these errors are classified as syntax errors, meaning spelling errors.
Syntax form
try {
// try ve throw ifadelerinin çalıştırılacağı blok;
} catch(error) {
// catch içerisinde yapılmasını istediğiniz kod blogu
}
Not: throw çalıştığı zaman bloktan çıkılır ve catch bloğuna gider. Bu sebepten dolayı throw ifadesinin altına yazacağınız kodlar çalışmayacaktır .
Let’s look at the following try, catch, and throw example;
var sayi = 40;
try {
if(sayi > 50) {
console.log('Başarılı');
}
else {
// the error message we defined
throw new Error('Sayı kucuk');
}
// because throw runs, the code below is not executed.
console.log('merhaba');
}
catch(error) {
console.log('sayı yakalandı');
console.log('Hata: ' + error);
}
If we explain our example, we used the throw statement inside the try block to catch the error. And the throw statement directed it to the catch code block and printed the error message to the console. However, there is one more thing to pay attention to: after the throw statement ran, console.log(‘merhaba’) did not run. Because, as I said earlier, after the error is caught, it is directed to the catch block and the error message is recreated.
Instead of the error messages above, meaning the error messages we created ourselves, we can also use built-in error messages.
- TypeError
- SyntaxError
- ReferenceError
- EvalError
- InternalError
- RangeError
Örnek : throw new ReferenceError('this is reference error');
Let’s run our code in every case with JavaScript Finally
With JavaScript try catch, we learned how to run a code block and catch it if an error occurs. However, if there is code we want to run in every case, regardless of whether an error occurs, we should use it inside finally. For this, we can use an example like the following.
const sayi1 = 100;
const sayi2 = 'deneme';
try {
console.log(sayi1/sayi2);
console.log(deneme);
}
catch(error) {
console.log('Hata Yakalandı');
console.log('Hata Mesajı : ' + error);
}
finally {
console.log('Finally blogundaki kod hata oluşsa bile çalışır.');
}
We can increase our examples related to JavaScript try catch throw and finally.
Note
- How to create scheduled functions using JavaScript setTimeout.
- Easy nested for loop with JS
- Using JavaScript Reduce
- using javascript-break-continue
- What are JavaScript onClick and onChange events?
- Official website
Share it if it helped


