JavaScript try...catch In Hindi

📔 : Java Script 🔗

JavaScript में try...catch का use Exceptions को handle करने के लिए किया जाता है , ताकि program terminate न होकर normally run हो सके।


JavaScript try...catch Syntax

try 
{
  // code...
} 
catch(errorObj) 
{
  // error handling
}

जैसा कि आपने पिछले topic में पढ़ा होगा कि , जब में Run Time Error आती है तो JavaScript उस Error का Object return करती है , जिसकी main properties - name (error name) और message (error message) होती हैं।


तो try{ } block के अंदर हम अपना सारा code / logic लिखते हैं और अगर जब भी कोई Run Time Error आती है तो us error को हम catch{} block में handle करते हैं जहां हमें उस Errorका Object मिलता हैं।

और अगर कोई Error नहीं आती तो program normal flow में run होता है।


JavaScript try...catch Example

File : trycatch.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Exception Handling Using try..catch 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}`); /* print Error message */
      }
    </script>
  </body>
</html>
Output
Error Name : ReferenceError
Error Message : blah is not defined  

? Backticks (``) का use करके string में हम ${ } ( dollar curly braces) के अंदर हम direct variables को print करा सकते हैं। और <br> का use line break करने के लिए करते हैं।

Explanation : दिए गए example में blah variable print कराने की कोशिश की गयी है जो कि undefined है , Error आते ही हम catch block में move हो गए जहाँ error name और message print कराया गया है।

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

try
{
  document.write(x;
}
catch(ErrorObj)
{
  /*we can't handle it*/
}

try...catch works Synchronously

JavaScript में try...catch synchronously work करती हैं , means हम try ...catch के through asynchronous error handle नहीं कर सकते हैं।

for example :

<script type="text/javascript">
try
{
setTimeout(function()
{
anyvariable; /* script will terminate here */
}, 1000);
}
catch (eroor)
{
alert( "it will not work" );
}
</script>

चूंकि setTimeOut() एक Asynchronous function है , और जब तक यह function run होता है तब तक js engine try...catch block को छोड़ चुका होता है।

What should we do ?

इस तरह के Asynchronous function में exception handle करने के लिए हमें try...catch का use function के अंदर ही करना पड़ेगा।

File : trycatch2.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Exception Handling for asunchronous code In Hindi </title>
  </head>
  <body>
    <script type="text/javascript">      
      setTimeout(function(){
        try {
            document.write(anyvariable); /* script will terminate here */
        } 
        catch(error) {
          document.write(error);
        }
      }, 1000);
    </script>
  </body>
</html>
Output
ReferenceError: anyvariable is not defined  

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