javaScript try catch

the error object contains information about errors.

javascript error object

whenever a run-time error occurs, an instance of the error object is created to describe the error. the error object can also be explicitly created or thrown using the throw statement.

           var     error_value  = new  error(message)

error types in javascript

  • syntax errors - occur when you write code that somehow violates the rules of the javascript language.syntax errors are generally fatal in the sense that they are errors from which the interpreter cannot recover.
  • runtime errors - occur while the script is running.these errors result from javascript that has the correct syntax but that encounters some sort of problem in its execution environment.
  • semantic errors - occur when the program executes a statement that has an effect that was unintended by the programmer.these errors much harder to catch.

error object properties

propertydescription
messagestring describing the nature of the exception
namestring indicating the type of the string

javaScript try catch and finally

the try block allows you to define a block of code to be tested for runtime-error while it is being executed.

note: the try block must be followed by exactly one catch or one finally(or one of both).

the catch clause is a block of code that is only executed if an exception occurs in the try clause.if no exception occurs within the 'try' block then 'catch' clause is skipped. it is optional.

the finally block is executed whenever the try or catch block finished.it is optional clause.

syntax: there are three forms of the 'try' statement:

try{
   
           //try______statements
}
catch(error){
    
          // catch______statement
}     
finally{
       
          // finally_______statements 
      
   }                
try
   { 
      
      //try______statements
      
    }
     catch(error){
    
    
    // catch______statement

                    }
try
   {
   
      //try____statements
}
 finally
        {
    
   //finally_____statement

       }


JavaScript try catch example

in the below example the 'method undefined' error will occurs in the try block and this catch by the 'catch' block and execute code inside the catch block.

      function   message(){
      
    
     try{     
     
            prints();         // prints method undefined in script so its generate runtime error
                            
         }
          catch(error)
                         {
                          
                  var  str  = "the some error will generate inside the try block \n\n";
                                         
                        str += "this error type   _______"+error.name+"\n\n";
                                              
                        str += "the error message________"+error.message+"\n\n";
                                              
                        str += "thanks for  using try catch clause\n";
                                              
                        alert(str);
                   } 
          }

the finally block guaranteed executed in any situation.look this example:

    function    callme(){
      
    
     try{     
     
             demo();      // demo method undefined inside script so its generate runtime error
                            
         }
          catch(error)
                         {
                          
                          alert("i am catch block alert i handle some errors");
                        
                         }
                          finally
                                 {
                                            var  str   = "this is  finally block";
                                            
                                                  str += "its executed in any situation";
                                                 
                                                  alert(str);   }            
          }

javascript throw statement

the javascript throw statement is used to generate exceptions.

If you use the throw statement together with try and catch, you can control program flow and generate accurate message displayed.

    throw    statement;

note : with throw statement no restriction on the type of data that can be thrown as an exception.

    function   message(){
      
    
     try{     
     
           var  digit = prompt("please enter a digit");
                         
           if(digit > 10){ throw  new error("the value grater than ten---"+digit);  }
                         
           if(digit < 10){ throw  new error("the value less than ten---"+digit);  }
                                                     
         }
          catch(error)
                         {            var   str = "this is the catch for handle error\n\n";
                                            
                                              str += "message------"+error.message+"\n\n";
                                              
                                              str += "error type-----"+error.name+"\n\n";
                             
                                              alert(str);
                        
                         }
                          finally
                                 {
                                            var  str   = "this is  finally block";
                                            
                                                  str += "its executed in any situation";
                                                 
                                                  alert(str);   }            
          }

note :- you can throw any value you like, including primitive , numbers and strings, but creating and then throwing an error instance is the preferable strategy.