9 Replies Latest reply on Feb 22, 2013 9:01 AM by gkaiseril

    Error handling -- what am I doing wrong?

    scifiguy20904

      When I run the following code in the console:

       

      try {
      var x=blah();
      console.println(x);
      }
      catch(e) {
      console.println("error!");
      }

       

      I get:

       

      blah is not defined
      2:Console:Execerror!

       

      I thought the try {} wasn't supposed to print out the error.

        • 1. Re: Error handling -- what am I doing wrong?
          try67 MVP & Adobe Community Professional

          This is a compilation error, not a run-time error. The try-catch clause

          only works on run-time exceptions.

          • 2. Re: Error handling -- what am I doing wrong?
            scifiguy20904 Level 1

            is there a way to shut off output temporarily on complilation errors?  Or to check for the existence of the funciton first before using it?   This is really about funcitons being called from the .js for people who don't have the .js loaded.  I want to be able to define the error message they see...

            • 3. Re: Error handling -- what am I doing wrong?
              gkaiseril MVP & Adobe Community Professional

              Only if you print the trapped error variable!

               

              What is the parameter "e" in the try catch statement?

               

              try {

              var x=blah();

              console.println(x);

              }

              catch(e) {

              var eMsg = "error!\n";

              if(app.viewerVerson < 6) {

              eMsg += e.toString();

              } else {

              for (var i in e)

              eMsg += i + ": " + e[i] + "\n";

              } // end if version

              app.alert(eMsg, 0, 0);

              }  finally { // end catch

              app.alert("I am done!");

              } // end try

              • 4. Re: Error handling -- what am I doing wrong?
                try67 MVP & Adobe Community Professional

                No, and why would you want to do that? What you can do, though, is turn off

                the option to show the console on warnings and errors...

                • 5. Re: Error handling -- what am I doing wrong?
                  scifiguy20904 Level 1

                  Why would i want to tell users to contact me if their protected folder .js is not loaded?   Well, because then I need to help them load it.  I'm not sure I understand your question.

                  • 6. Re: Error handling -- what am I doing wrong?
                    try67 MVP & Adobe Community Professional

                    There are other ways of doing that. You can do something like this:

                     

                    if (typeof blah=="undefined") {

                    app.alert("You must install the folder script first!")

                    } else {

                    // execute code

                    blah();

                    }

                    • 7. Re: Error handling -- what am I doing wrong?
                      Test Screen Name Most Valuable Participant

                      If it doesn't compile, it won't run...

                       

                      I've never done it, but one way to avoid compilation errors is to defer compilation to runtime by using eval.

                      • 8. Re: Error handling -- what am I doing wrong?
                        gkaiseril MVP & Adobe Community Professional

                        With more coding in the catch statement one can even select how to process different types of errors.

                         

                        try {   
                           var x=blah();
                           // may throw four exceptions
                        } catch (e if e instanceof TypeError) {
                           // statements to handle TypeError exceptions
                           app.alert("TypeError\n" + e.toString(), 0, 0);
                        } catch (e if e instanceof RangeError) {
                           // statements to handle RangeError exceptions
                           app.alert("RangeError\n" + e.toString(), 0, 0);
                        } catch (e if e instanceof EvalError) { 
                           // statements to handle EvalError exceptions
                           app.alert("EvalError\n" + e.toString(), 0, 0);
                        } catch (e if e instanceof ReferenceError) { 
                           // statements to handle EvalError exceptions
                           app.alert("Reference error\n" + e.toString(), 0, 0);
                        } catch (e) {   
                           // statements to handle any unspecified exceptions   
                           app.alert("UnclasifiedError\n" + e.toString(), 0, 0);
                           }

                        • 9. Re: Error handling -- what am I doing wrong?
                          gkaiseril MVP & Adobe Community Professional

                          You could also use:

                           

                          if(typeof blah != "function") {

                          app.alert("Missing special \"blah\" function,\nPlease contact Joe Doe, System Administrator, ext. 000", 0, 0);

                          }

                           

                          This would eliminate any variable or other object with the name of "blah" form preventing the detection of the missing function.

                           

                          var blah = "Blah";

                          if(typeof blah != 'function') app.alert("blah not a function\ntypeof blah = " + typeof blah);

                          if(typeof blah != 'object') app.alert("blah not an object\ntypeof blah = " + typeof blah);

                          if(typeof blah != 'string') app.alert("blah not a strng\ntypeof blah = " + typeof blah);

                          if(typeof blah != 'number') app.alert("blah not a number\ntypeof blah = " + typeof blah);

                          if(typeof blah == 'undefined') app.alert("blah is defined\ntypeof blah = " + typeof blah);

                          if(typeof blah != 'undefined') app.alert("blah not defined\ntypeof blah = " + typeof blah);