Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




JAVASCRIPT - CUSTOM EXCEPTION


Although we have seen the Exceptions provided by JavaScript. But sometimes, we need to create our own Exception.


And luckily, JavaScript provides an throw statement, that can help you define your own exception.


Create a Custom exception


Example :



<html>
<body>  
<script>

	var number = 23
    try {
		if (number < 50) {
			throw "Number cannot be less than 50"
		}        
    } catch(err) {
    	document.write("There is an error :: "+err)
    }
	    
</script>      
</body>
</html>


Output :



  There is an error :: Number cannot be less than 50

So, if you see the above output. We have raised the same exception, without defining a class.


All we have done is used the throw statement, passing the message, Number cannot be less than 50.


throw "Number cannot be less than 50"

And we were able to raise our custom exception.