JavaScript - try / catch statement
In this article, we would like to show you how to use try / catch statement in JavaScript.
There are four basic keywords related to the topic that you need to know:
try
- is used to test the code wrapped with curly braces ({}
) for errors,catch
- is executed if an exception is thrown in thetry
block,finally
- executes the code wrapped with curly braces after try... catch block regardless of the result,throw
- is used to create and throw custom errors.
Syntax / example | Description |
---|---|
xxxxxxxxxx 1 try { 2 // place for the code where exception 3 // can be thrown in the safe way 4 } catch(error) { 5 // this block is executed only if exception occurs 6 } | This solution catches exceptions that appear in the code in the try block. If the exception occurs, the catch is executed. |
xxxxxxxxxx 1 try { 2 // place for the code where exception 3 // can be thrown in the safe way 4 } catch(error) { 5 // this block is executed only if exception occurs 6 } finally { 7 // this block is executed always 8 // after try and catch blocks 9 // (even when exception occurs) 10 } | The same scenario as in the example above, with one difference - regardless of the exception being thrown (or not), the finally block will always be executed after the try and catch blocks. |
xxxxxxxxxx 1 try { 2 // place for the code where exception 3 // can be thrown in the safe way 4 } finally { 5 // this block is executed always 6 // after try and catch blocks 7 // (even when exception occurs) 8 } |
In this case, the exception is not caught by the Finally block always executes after a |
In this example, we present how to create a simple try
block that throws an error and how to handle the error with the catch
block.
Practical example:
xxxxxxxxxx
try {
let x = y;
} catch (error) {
console.error(error); // ReferenceError: y is not defined
}
In this section, we present how to throw a custom error (throw exception) using throw
statement.
xxxxxxxxxx
try {
let x = 3;
if (x != 0) {
throw 'Custom error message.'
}
console.log('Try block executed.');
} catch (error) {
console.error(error); // Custom error message.
}
Note:
You can
throw
different types of exceptions.
In this section, we present three different cases of finally
block execution.
1. When exception is thrown you can see both messages in the console.
xxxxxxxxxx
try {
const x = 3;
if (x != 0) {
throw 'Custom error message.'
}
console.log('Try block executed.');
} catch (error) {
console.error(error);
} finally {
console.log('Finally block message (executed on success and exception).');
}
2. When exception isn't thrown you can see the finally
block message in the console.
xxxxxxxxxx
try {
// insert your code here...
console.log('Try block executed.');
} catch (error) {
console.error(error);
} finally {
console.log('Finally block message (executed on success and exception).');
}
3. When exception is thrown but not catched, the finally
block will still execute.
xxxxxxxxxx
try {
const x = 3;
if (x != 0) {
throw 'Custom error message.'
}
console.log('Try block executed.');
} finally {
console.log('Finally block message (executed on success and exception).');
}