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 354 views
1 answer 214 views
1 answer 146 views
146 views asked Oct 8, 2022 by avibootz
2 answers 210 views
210 views asked Nov 22, 2020 by avibootz
2 answers 301 views
...