How to use throw statement in catch block with JavaScript

1 Answer

0 votes
function myFunction() { 
 
    var n;
     
    try { 
        if(n == "")  throw "n is Empty";
        if(isNaN(n)) throw "n is not a number";
        if(n < 0)    throw "n is negative";
    }
    catch(err) {
        document.write(err + "<br />");
        document.write(err.message + "<br />");
    }
}
  
document.write(myFunction()); 
 
  
/*
run:
 
n is not a number
undefined
undefined 
 
*/

 



answered May 1, 2017 by avibootz
edited May 1, 2017 by avibootz

Related questions

4 answers 356 views
1 answer 215 views
1 answer 150 views
150 views asked Oct 8, 2022 by avibootz
2 answers 214 views
214 views asked Nov 22, 2020 by avibootz
2 answers 304 views
...