JavaScript try..catch..finally In Hindi

📔 : Java Script 🔗

पिछले topic में आपने JavaScript में try catch के बारे में पढ़ा , try catch का use करके Exception को handle किया। इस topic में हम finally block के बारे में पढ़ेंगे।


Exceptions को handle करने के लिए हम अपना code try block के अंदर लिखते थे , अगर error आती थी तो उस error को catch block में handle करते थे। कई बार ऐसी situation आती है कि code को दोनों conditions में Run करना हो , means Exceptions आये तब भी code Run हो और न आये तब भी, वहाँ पर हम finally block use करते हैं।


finally block , try या catch block के बाद हमेशा run होता है।

  • अगर कोई Exceptions नहीं है तो try block के बाद run होगा ,
  • और Exceptions आयी तो catch block के बाद।

? try catch की तरह ही try catch finally भी सिर्फ Run Time Errors (जिन्हे Exception कहते हैं) में ही काम करता है , Syntax Error या Logical Errors को नहीं। अगर program में syntactically error है तो उसके लिए यह काम नहीं करेगा।

JavaScript try catch finally Syntax

try 
{
   //write you actual code
} 
catch(errorObj) 
{
   //handle errors
} 
finally 
{
   //it will execute always
}

JavaScript try catch finally Example

File : trycatch.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Exception Handling Using try catch finally In Hindi </title>
  </head>
  <body>
    <script type="text/javascript">
      try
      {
        document.write(blah); /* here I have print undefined variable */
      } 
      catch(ErrorObj)
      {
        document.write(`Error Name : ${ErrorObj.name} <br>`);  /* print Error name */
        document.write(`Error Message : ${ErrorObj.message} <br>`); /* print Error message */
      }
      finally
      {
        document.write(`it will run always`);
      }
    </script>
  </body>
</html>
Output
Error Name : ReferenceError
Error Message : blah is not defined
it will run always

दिए गए example के output में आप देख सकते है की Exception के वावजूद भी try block Run हुआ है।

JavaScript try finally

well , ये जरूरी नहीं है कि आप catch block के बाद ही finally block use करें , अगर आ Exceptions handle नहीं करना चाहते हैं तो catch block को skip कर try block के बाद लिख सकते हैं।

File : tryfinally.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Exception  try finally In Hindi </title>
  </head>
  <body>
    <script type="text/javascript">
      try
      {
        document.write(`try block <br>`);
      }
      finally
      {
        document.write(`it will run always`);
      }
    </script>
  </body>
</html>
Output
try block
it will run always

हालाँकि इस case में भी अगर Exception नहीं आयी तो try finally दोनों run होंगे otherwise finally block run होगा ही।


Note - finally block हमेशा try block के बाद या try catch के बाद ही आएगा , आप इसे try finally catch नहीं लिख सकते हैं। या बिना try के catch finally नहीं लिख सकते हैं।नीचे कुछ valid / invalid syntax गए हैं।

/* Valid */
try{
//write you code here
}
finally{
// it runs always
}

/* Valid */
try{
//write you code here
}
catch(errorObj){
// handle error
}
finally{
// it runs always
}

/* Invalid : Uncaught SyntaxError: catch without try*/
catch(errorObj){
// handle error
}
finally{
// it runs always
}

/* Invalid : Uncaught SyntaxError: catch without try*/
try{
//your code
}
finally{
// it runs always
}
catch(errorObj){
// handle error
}

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers