17 Replies Latest reply on Dec 10, 2010 10:53 AM by gkaiseril

    Exception handling with try/catch in acrobat

    ForrestGimp Level 1

      Hi

       

      I have a problem using a try/catch block in my acrobat document-script. Try to enter the following into the debugger-console:

       

      try{nonexistentFunction();}catch(e){console.println('\nacrobat can't catch')}

      and run it. The output will be:

      nonexistentFunction is not defined
      1:Console:Exec
      acrobat can't catch

      true

      The whole point of a rty/catch block is for the application  NOT to throw an exception, but instead execute the catch-part of the  statement. However, acrobat does both: It throws an exception AND  executes the catch-block.

       

      Is there another way to suppress the exception, or to make the try/catch-block work as it's supposed to?

        • 1. Re: Exception handling with try/catch in acrobat
          Bernd Alheit Adobe Community Professional & MVP
          It throws an exception AND  executes the catch-block.

          Yes. When Acrobat doesn't throws an exception it doesn't executes the catch-block.

          • 2. Re: Exception handling with try/catch in acrobat
            try67 MVP & Adobe Community Professional

            Maybe it's because you have more than one error. Namely, you use single  quotes for your error message, but also for the word "can't".


            If you execute this code instead:
            try{nonexistentFunction();}catch(e){console.println("\nacrobat can't  catch")}
            you'll see that only the line you specified gets printed to the console.
            • 3. Re: Exception handling with try/catch in acrobat
              ForrestGimp Level 1

              @Bernd: Of course it throws an exception, but the exception message is not supposed to be there. Instead only the catch-block should be executed. As there's no exception in the code within the catch-block only the message "acrobat can't catch" should appear.

               

              @try67: You're partly right. My original testmessage was a bit more negative (considering where to stick acrobat), which I found inappropriate to post here. It didn't have an apostrophe in it.

              Still, if you test it with

              try{nonexistentFunction();}catch(e){console.println("\nacrobat can't  catch")}

              as you suggest, it will (at least in Acrobat Pro Extended 9.1.0) respond with

              nonexistentFunction is not defined
              1:Console:Exec
              acrobat can't  catch

              true

              The information I have about the try/catch and how it should work is from this page: http://acrobatusers.com/tutorials/2008/12/js_exception_handling

              • 4. Re: Exception handling with try/catch in acrobat
                try67 MVP & Adobe Community Professional

                I run this code and all I got in the console was "acrobat can't catch"...

                (Reader 9.3.0)

                • 5. Re: Exception handling with try/catch in acrobat
                  ForrestGimp Level 1

                  Reader has a console where you can enter text? Where is that one hidden?

                   

                  As for the problem: could it be that acrobat pro extended handles exceptions differently than reader, and hence will always show its own exception-messages as well as executing the catch-block?

                  • 6. Re: Exception handling with try/catch in acrobat
                    try67 MVP & Adobe Community Professional

                    No it doesn't, which I why I built one...

                    http://try67.blogspot.com/2010/05/reader-run-javascript-code-from-within.html

                     

                    It's possible that Acrobat and Reader handle exceptions differently,

                    although I find it somewhat odd.

                    Do you see the exception in the console if you don't write something to it

                    yourself? Like if you use an alert instead?

                    • 7. Re: Exception handling with try/catch in acrobat
                      ForrestGimp Level 1
                      try{none();}catch(e){app.alert("this is an exception");}

                      and

                       

                      try{none();}catch(e){}

                       

                      both show the acrobat exception response in the console in pro extended.

                       

                      By the way, I just found out that there is indeed a full console hidden in reader 9.4. It can be set to show up on errors in the user-preferences. I was able to enter and execute code in it, once it had shown up.... Funny. I never even looked for one in reader.....

                       

                       

                      Edit: must have mixed that up somehow.... It is NOT possible to execute code in this console. But it will display errors.

                      • 8. Re: Exception handling with try/catch in acrobat
                        ForrestGimp Level 1

                        Sorry for the double-post, but this seems to be important:

                         

                        1. I have indeed confirmed, that acrobat pro extended would always display its exception-message in the console, regardless of any catching I tried to implement.

                         

                        2. However, Reader will not. Reader only displays console-messages for uncaught exceptions.

                         

                        This doesn't make too much sense (as you've hinted already).

                        I can provide a demo-file illustrating this behaviour.

                        • 9. Re: Exception handling with try/catch in acrobat
                          Bernd Alheit Adobe Community Professional & MVP

                          @Bernd: Of course it throws an exception, but the exception message is not supposed to be there.

                          Why did you assume this?

                          • 10. Re: Exception handling with try/catch in acrobat
                            gkaiseril MVP & Adobe Community Professional

                            Of course it runs, your code has an error, there is no "nonexistentFunction".

                             

                            Try this code:

                             

                            try{
                            app.alert("This button tries to run a non-existent function",0, 0);
                            nonexistentFunction();
                            }catch(e){
                            console.show();
                            console.clear();
                            console.println("\nacrobat can't  catch");
                            console.println("\n");
                            if(app.viiewerVersion < 6) {
                               app.alert('e string:' + e.toString());
                            } else {
                               for(i in e) {
                               console.println(i + ': ' + e[i]);
                               } // end for in
                               } // end if version viewer
                            } // end catch

                             

                            For no error in a try this code:

                             

                            try{
                            app.alert("This button has no error",3, 0);
                            console.show();
                            console.clear();
                            }catch(e){
                            console.show();
                            console.clear();
                            console.println("\nacrobat can't  catch");
                            console.println("\n");
                            if(app.viiewerVersion < 6) {
                               app.alert('e string:' + e.toString());
                            } else {
                               for(i in e) {
                               console.println(i + ': ' + e[i]);
                               } // end for in
                               } // end if version viewer
                            } // end catch

                             

                             

                            For a user forced error try this code:

                             

                            try{
                            app.alert("This button throws a user set error",3, 0);
                            throw(["User Forced Error"]);
                            console.show();
                            console.clear();
                            }catch(e){
                            console.show();
                            console.clear();
                            console.println("\nacrobat can't  catch");
                            console.println("\n");
                            if(app.viiewerVersion < 6) {
                               app.alert('e string:' + e.toString());
                            } else {
                               for(i in e) {
                               console.println(i + ': ' + e[i]);
                               } // end for in
                               } // end if version viewer
                            } // end catch

                             

                            For versions of Reader Adobe provides the JS Debugger and registry changes to enable the JS console in those versions of Reader that do not have a JS console. Using the app.alert could be a better overall approach for a procuction form. See testdebugger8.zip (Mac/Win, 6K)

                            • 11. Re: Exception handling with try/catch in acrobat
                              dying veteran Level 1

                              so whats the conclusion of this confusing thread ?

                              • 12. Re: Exception handling with try/catch in acrobat
                                try67 MVP & Adobe Community Professional

                                I don't understand what the big confusion is all about. ForrestGimp's point

                                seems quite clear to me.

                                It boils down to this: Exceptions thrown in Acrobat will always* create an

                                output in the console, even if caught. In Reader they do not.

                                I think the Reader implementaion is the correct one and would recommend

                                reporting this to Adobe as a bug.

                                 

                                * I'm actually still not sure this is completely true. I think it might also be related to the kind of exception that is thrown.

                                • 13. Re: Exception handling with try/catch in acrobat
                                  gkaiseril MVP & Adobe Community Professional

                                  It provides examples of the 'try catch'  statement and some of the details that can be tweaked out of the trapped error.

                                   

                                  The first example is for you issue when a non-existing function is called. It works in both Acrobat and Reader.

                                   

                                  There is an example of what happens when an existing function is called with a missing parameter.

                                   

                                  The last example shows how to create you own error and a unique error message.

                                   

                                  Since the JS console is not always accessible by Reader, then a creator should consider using the 'app.alert' for a public used form when catching and processing an error. Not only will the cryptic JS console not show, but the programmer can custom format the text being displayed and the message could be in a more understandable format.

                                   

                                  Also Adobe provides for free the JS or compiled file for Acrobat Reader to support the JS console.

                                  • 14. Re: Exception handling with try/catch in acrobat
                                    dying veteran Level 1

                                    > Also Adobe provides for free the JS or compiled file for Acrobat Reader to support the JS console.

                                     

                                    Where is that file located ? How to install it or where to place it ?

                                     

                                    What is the method referred by try67 on his site where he sells a product ?

                                     

                                    Is that the same as the compiled file you refer to ? or did he sell his solution to adobe ?

                                     

                                    It is helpful if people can get an idea of the nature of choices available and make informed decisions, than a cloak and dagger approach.

                                     

                                    ..............

                                     

                                    For some jobs that we have, I have been very frustrated by a consultant who wont even give basic info for transparent billing despite all assurances for privacy, as a result we are forced to do the job ourselves.

                                     

                                    Dying Vet

                                    • 15. Re: Exception handling with try/catch in acrobat
                                      gkaiseril MVP & Adobe Community Professional

                                      Currently Adobe is only providing the version 8 version. Go to JavaScript for Acrobat in the Acrobat section of the Developer network, and scroll down to version 8. There is a link to download a ZIP file with the JS code for the Debugger. This is placed in the Acrobat JS application folder and

                                       

                                      Developing Acrobat Applications Using JavaScript has information for modifying the Windows resistry

                                      • 16. Re: Exception handling with try/catch in acrobat
                                        ForrestGimp Level 1
                                        The first example is for you issue when a non-existing function is called. It works in both Acrobat and Reader.

                                         

                                        Of  course it works, because the first thing you do in the catch-block is  to clear the console. Imho this should not be necessary, as acrobat  should ignore caught exceptions, and just execute the catch-block.

                                         

                                        [...]It boils down to this: Exceptions thrown in Acrobat will always* create an

                                        output in the console, even if caught. In Reader they do not.

                                        I think the Reader implementaion is the correct one[...]

                                        Exactly.

                                        When I deliberately catch an exception it should trigger ONLY the code in the catch block to be executed, and nothing more. No additional console-output, no warning-messages. Just the catch.

                                         

                                        @ try67: You might be right. Unfortunately I have no time to get into this any deeper, like testing all kinds of exceptions

                                        • 17. Re: Exception handling with try/catch in acrobat
                                          gkaiseril MVP & Adobe Community Professional

                                          See trycatch.pdf for a working example with a couple of different errors. If you open the console, you should note that not all errors create console output when the error is captured to a variable and outputted using an 'app.alert'.  The console will always open if one selects either the 'Trace' or the 'Break' options for handling "When exception is thrown" preference.

                                           

                                          You can use the typeof operand to see if your function is a function and not throw a serious system error and does not write anything to the JS console. See the following example:

                                           

                                          var nIcon =  3;
                                          var nType = 0;
                                          if(typeof(MyFunction) == 'function') {
                                             cMsg = 'No error will occur because MyFunciton exist';
                                             }
                                          else {
                                             cMsg = 'An error will occur because MyFunction does not exist!\n\nContinue to run the call for MyFunction?' ;
                                             nIcon = 2;
                                             nType = 2;
                                          }
                                          app.alert(cMsg, nIcon, nType);