Skip to main content

Command Palette

Search for a command to run...

single try and multiple error handling in javascript

Updated
•1 min read•View as Markdown
K

I’m Kailas, a passionate coder who started with PHP and WordPress in 2017 and now enjoys building scalable systems using Go, Node.js, AWS, Python, Redis, SQL, microservices, solving DSA in C++ and Python, and prototyping clean UI ideas in Figma before building.

Unlike java in javascript we have only single block to handle error

try {
    // un-comment line below to see (reference error)
    // show();

    // un-comment line below to see (type error)
    // a = "".toFixed()
} catch (e) {
    if (e instanceof TypeError) {
        // handle type error
        console.log("You have type error");
    } 
    else if (e instanceof ReferenceError) {
        // handle reference error
        console.log("You have reference error");
    } 
    else{
        console.log("other error")
    }
}