Exceptions can be caught with standard try ... catch
blocks like in this example. There is a bug in our return8()
function that is discovered by the T6.eq()
method. Since we caught the error, the script continues to run normally.
Follow these steps to create a new project workspace and install the t6 dependency to run this example.
# Create and open project folder
mkdir Catching_Exceptions_demo
cd Catching_Exceptions_demo
# Initialize project and install dependencies
npm init -y
npm i t6@1.1.9
# Create and open source file
touch "Catching Exceptions.mjs"
open "Catching Exceptions.mjs"
Copy and paste this source code into Catching Exceptions.mjs
.
import { T6 } from 't6';
// Oops! There was a logic
// error in returns8()!
function returns8() {
return 9;
}
try {
// This test will fail
T6.eq(returns8(), 8);
} catch (e) {
console.log(e.message);
}
console.log('...Still processing...');
In Catching_Exceptions_demo/
, execute Catching Exceptions.mjs
with NodeJS to generate an output.
node "Catching Exceptions.mjs"
You should expect to see an output similar to the one below.
Exception found in test #1! The test value of 9 was not equal to the expected value of 8.
...Still processing...