how to detect if try/catch exception handling is supported in browser

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • adnanx82@gmail.com

    how to detect if try/catch exception handling is supported in browser

    Hi,

    Does anyone know how a script can detect whether exception handling
    using try/catch blocks is supported in the browser, so that it can use
    them to perform better error handling, and a syntax error is not
    produced in cases where they are not supported?

    I know that IE and Netscape 5 and above support exception handling (and
    older versions don't), but I don't know about other browsers.

    Thanks,

    -Adnan.

  • McKirahan

    #2
    Re: how to detect if try/catch exception handling is supported in browser

    <adnanx82@gmail .com> wrote in message
    news:1116618469 .715466.216690@ g43g2000cwa.goo glegroups.com.. .[color=blue]
    > Hi,
    >
    > Does anyone know how a script can detect whether exception handling
    > using try/catch blocks is supported in the browser, so that it can use
    > them to perform better error handling, and a syntax error is not
    > produced in cases where they are not supported?
    >
    > I know that IE and Netscape 5 and above support exception handling (and
    > older versions don't), but I don't know about other browsers.
    >
    > Thanks,
    >
    > -Adnan.
    >[/color]

    Apparently try/catch was added in JavaScript 1.4 (or 1.5) per


    JavaScript Version Incompatibiliti es

    has a script to tell you what JS version your browser supports.

    Is there an exception/error handling mechanism in core JavaScript?

    describes what you want (I think).


    Comment

    • Kenneth Tbibetts

      #3
      Re: how to detect if try/catch exception handling is supported in browser

      If you test the 'typeof' one of the Error classes that is supported in the
      version 3 of EcmaScript you can have high confidence that try-catch is also
      supported. The return value will be 'function'. A return value of Object
      means the null object, and no support. For instance:

      if(typeof(windo w.TypeError)==' function'){
      go ahead and include try catch code.
      }

      Warning- I remember Netscape 4 would have conniptions if I even included the
      word 'try' in the source code.
      Netscape choked on try being a protected keyword even if it never got
      called.

      I test the client support for document.implem entation and write a script
      element in the head, while the page loads.
      You can add a filter for try-catch; but so far every client that passes the
      document.implem entation test is OK for try catch.

      remote script loaded in the document.head as <script type="text/javascript"
      src=" test.js"></script>

      file test.js:
      function loadWch(boo){
      var DI= document.implem entation;
      if (DI && DI.hasFeature && DI.hasFeature(' html', '1.0')){
      var str= (boo)? 'someCodefilepa th': 'otherCodefilep ath';

      str='<script type= "text/javascript" + src= "'+ str+ '" > <\/s';
      document.writel n(str+'cript>') ;
      }
      }
      loadWch((typeof (window.TypeErr or)=='function' ));


      Comment

      • Kenneth Tbibetts

        #4
        Re: how to detect if try/catch exception handling is supported in browser

        If you test the 'typeof' one of the Error classes that is supported in the
        version 3 of EcmaScript you can have high confidence that try-catch is also
        supported.

        The return value will be 'function'. A return value of 'object '
        means the null object, and no support. For instance:

        if(typeof(windo w.TypeError)==' function'){
        go ahead and include try catch code.
        }

        Warning- I remember Netscape 4 would have conniptions if I even included the
        word 'try' in the source code.
        Netscape choked on try being a protected keyword even if it never got
        called.

        I test client support for document.implem entation before I include scripts
        on a page.

        You can add a filter for the Error Type; but so far every client that passes
        the
        document.implem entation test is OK for try catch.

        remote script loaded in the document.head:
        <script type="text/javascript" src=" test.js"></script>

        file test.js:
        function loadWch(boo){
        var DI= document.implem entation;
        if (DI && DI.hasFeature && DI.hasFeature(' html', '1.0')){
        var str= (boo)? 'someCodefilepa th': 'otherCodefilep ath';

        str='<script type= "text/javascript" + src= "'+ str+ '" > <\/s';
        document.writel n(str+'cript>') ;
        }
        }
        loadWch((typeof (window.TypeErr or)=='function' ));


        Comment

        • adnanx82@gmail.com

          #5
          Re: how to detect if try/catch exception handling is supported in browser

          Thanks for the replies.

          What do you guys think of this solution (below)? To simulate the test
          for a browser in which try is not implemented, change the line with
          "eval('try {......." to some invalid syntax like "eval('cry {....."
          .....

          Thanks,

          -Adnan.
          ---------------------
          <html>
          <script>
          var trysupport = 'no';

          function on_error(msg, url, line)
          {
          if (trysupport == 'testing')
          {
          alert ('try/catch not supported');
          }
          else
          {
          alert ('some other error');
          }
          }
          window.onerror = on_error;

          function checkTrySupport ()
          {
          trysupport = 'testing';
          eval('try { trysupport = "yes"; } catch (e) {}');
          alert('try/catch supported');
          }

          checkTrySupport ();
          </script>
          <body>
          TRY SUPPORT TEST PAGE
          </body>
          </html>

          Comment

          • Csaba Gabor

            #6
            Re: how to detect if try/catch exception handling is supported in browser

            adnanx82@gmail. com wrote:[color=blue]
            > Hi,
            >
            > Does anyone know how a script can detect whether exception handling
            > using try/catch blocks is supported in the browser, so that it can[/color]
            use[color=blue]
            > them to perform better error handling, and a syntax error is not
            > produced in cases where they are not supported?[/color]

            There was a March 31 thread on this titled:
            How to test for try/catch support


            Csaba Gabor from Vienna

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: how to detect if try/catch exception handling is supported in browser

              adnanx82@gmail. com wrote:
              [color=blue]
              > Does anyone know how a script can detect whether exception handling
              > using try/catch blocks is supported in the browser, so that it can
              > use them to perform better error handling, and a syntax error is not
              > produced in cases where they are not supported?[/color]

              This is one of the rare occasions where eval() really comes in handy.
              Browsers with script engines that do not support exception handling
              (try...catch) have a global `onerror' event handler (specified as part
              of the core language instead of the DOM in JavaScript prior to v1.4)
              that can be used.

              In global context:

              var _global = this;


              Anywhere "before" the following code is executed:

              function dummyError()
              {
              _global.onerror = null;
              return true;
              }

              In local context:

              _global.onerror = dummyError;

              eval(new Array(
              'try {',
              ' // do what you want',
              '} catch (e) {',
              ' // handle e',
              '}').join("\n") );

              dummyError();


              PointedEars

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: how to detect if try/catch exception handling is supported in browser

                adnanx82@gmail. com wrote:
                [color=blue]
                > What do you guys think of this solution (below)? To simulate the test
                > for a browser in which try is not implemented, change the line with
                > "eval('try {......." to some invalid syntax like "eval('cry {....."[/color]

                It is a nice example and the right approach, however it is invalid HTML[1]
                and, more important, will fail compilation in an ECMAScript < 3 script
                engine if any try...catch block is included later to accomodate the
                detected support; to be seen with Netscape Navigator 4, for example.

                I am afraid that it is FWIS not possible to make that feature downwards
                compatible in a way that you can execute code that would compile in a
                ECMAScript 3+ compliant script engine only if such an engine is present.

                However, it is entirely possible to force the ECMAScript 3+ compliant
                code via eval() to compile and handle the error when fails to compile.
                See my other posting[2] for that.


                PointedEars
                ___________
                [1] <http://validator.w3.or g/>
                [2] <1342040.NUXyYe 4XEO@PointedEar s.de>

                Comment

                Working...