Error handling with try catch

const user = { email: 'jdow@gmail.com'};

try {
    // Produce a reference error
    // myFunction();

    // Produce Type error
    // null.myFunction();

    // Will produce syntax error
    //console.log(eval('Hello World'));

    // Will produce a URI error
    // decodeURIComponent('%');

    if(!user.name){
        // throw 'User has no name'
        throw new SyntaxError('User has no name');
    }

} catch(e){
    console.log(`User Error: ${e.message}`);
    
    console.log(e);
    // console.log(e.message);
    // console.log(e.name);
    // console.log(e instanceof ReferenceError);
} finally {
    console.log('Finally runs regardless of result...');
}

console.log('Program continues ...');


Was this page helpful?

Reader Interactions

Leave a Reply

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