Try catch throw with doScript()

Hi,
Today is my InDesign Scripting Bug Day... grrr. In the following example, the Error e in line 6 is undefined. When I run the Function run() without doScript() it works as expected. It looks like, throw isn't implemented correct with doScript(). At least the throw statement works but does not carry the Error. This is not working in CS4 and CS5.5.
try {
    app.doScript(run, ScriptLanguage.JAVASCRIPT , undefined, UndoModes.ENTIRE_SCRIPT, "PX_TEMP");
//~    run()
catch (e) {
  $.writeln("Caught: " + e.message);
function run() {
        throw new Error("Im an error");

The try...catch structure is very specific in that it creates (and destroys) at runtime its own variable scope to target the Error object. I don't know exactly how app.doScript is implemented but there are some reason to believe that the error thrown in the inner script cannot go back to the outer try...catch scope. The error object is probably lost when the inner script returns.
An ugly solution is to catch the error within app.doScript, to backup the object in a safe place and to re-throw the same error in the outer try block:
function run(){ throw new Error("Im an error"); }
run.error = null;
try {
    app.doScript('try{ run(); } catch(eInner){ run.error=eInner; }');
    if( run.error ){ throw run.error; }
    // you can throw other errors here
catch(e)
    alert( "Caught: " + e );
But I hope you have a really good excuse to do such a thing!
@+
Marc

Similar Messages

  • Try-catch & throw Exception Question

    Method evenNumber can throw IOException, and the caller of method
    evenNumber (in this case, method evenNumberTest and method evenNumberTest2)
    should catch the IOException. Otherwise, it will have compile
    error as the one in method evenNumberTest2.
    My question is when I run the program, it will have output "catch evenNumberTest",
    but it doesn't output "throw testNumber" Why is that?
    I thought ""throw new IOException("throw testNumber");"" will print
    "throw testNumber" when it throws the IOException.
    So in what situation "throw testNumber" will print it out??
    Please advise. Thanks!!
    =======================================================
    import java.io.*;
    public class ExceptionTest
    private void evenNumberTest()
    {     try
         { evenNumber(2);
         catch(IOException e)
         { System.out.println("catch evenNumberTest");
    ExceptionTest.java:14: unreported exception java.io.IOException; must be caught
    or declared to be thrown
    {   evenNumber(2);
    ^
         private void evenNumberTest2()
    {     evenNumber(2);
    private void evenNumber(int x) throws IOException {
         if (x % 2 == 0)
         throw new IOException("throw testNumber");
    public static void main(String[] args)
    {     ExceptionTest e = new ExceptionTest();
         e.evenNumberTest();

    Exception messages aren't printed out when the
    Exceptions are thrown.
    They are only printed if you explicitly print them, or
    if you do not catch them.He is talking about this print statement within the catch block
    catch(IOException e)
    { System.out.println("catch evenNumberTest");

  • Try/catch/throw

    I get confused on the try/throw/catch relationship. The way I read it, try trys to do a bit of code, and if it errors, the catch would be called for the specific error and what to do about it, but does the throw specify a new catch in the try section?
    Would the throw work the same if it was coded as a completely different catch, or is the throw based on a different type of error (logic error versus user error)?
    try
    to do this
    if (something bad happens)
    throw (specific error to go to a new catch)
    catch (What went wrong)
    what to do about it
    catch (new catch)
    what to fix the specific error
    I would appreciate any help....Thanks

    Regarding Paternostro's response:
    So, is it true that the exception is passed up the
    calling stack and will be caught by some other catch
    block?It is true that the exception is passed up the calling stack, but it will only be caught if the caller catches it. In the earlier example, it was not necessary that setLastName( ) declare that it throws an IllegalArgumentException because that is a runtime exception. Only checked exceptions need to be caught or declared as being thrown.
    So, if setLastName( ) threw say, an Exception, then it would have to declare that it throws it in the method declaration, and either it or the caller would have to catch for it or declare that it, in fact, throws Exception. The checks would keep propagating up until it is either handled or unhandled.
    Jeff

  • Please help with try/catch

    Hello, I'm writing a program and I got surprised with the following. I've got the following try/catch block:
    try {
    String param = config.getConfigValue("db2.driver");
    if (param == null)
    // parameter not defined.
    throw new Exception("Parameter 'db2.driver' missing!");
    else
    Class.forName(param);
    catch (SQLException sqlEx) {
    // do something
    catch (Exception e) {
    System.out.println("*** ERROR loading driver..."); (1)
    finally {
    releaseDB2Resources();
    I know the program flow goes through the finally block, but it doesn't print the message (1). Why is that? I don't understand it. What am I doing wrong?
    Thanks in advance.

    A finally block always gets executed, so that's why the println always works there. The only reason why the println in the catch wouldn't work is if that exception wasn't thrown. If a config value is not there, it might be the null string "" instead of null. Try this:
    if (param.equals(""))
    throw new Exception("blah");
    You could also try:
    if (true)
    throw new Exception("blah");
    to make sure that the exception is caught where you think it should be.

  • Need HELP with finally() in nested try-catch

    hi,
    i am having trouble with deadlock and wondering if its due to an incorrect use of finally nested within multiple try catch blocks.
    is the inner finally() required, will the outer one get executed, or will the two finally() statements get executed...??
    the deadlock happens very infrequently to accurately test.
    try {
         try {
         catch (InterruptedException e) {
              return;
    catch {
    finally {
         //will this be executed be executed by the inner throw statement
    }or is an inner finally required
    try {
         try {
         catch (InterruptedException e) {
              return;
         //this finally is NEW....is it needed!
         finally {
              //will this be executed. will both get executed
    catch {
    finally {
         //will this be executed also, or completely ignored if exception throw from inner try/catch
    }

    inner/outer, which one is executed first?
    more info pls.I mean, really. How hard is it to find out for yourself?
    public class TestFinally {
        public static void main(String[] args) {
            try {
                try {
                    throw new RuntimeException("Whatever");
                } finally {
                    System.out.println("Inner finally");
            } finally {
                System.out.println("Outer finally");
    }I'll leave it to your imagination as to how to modify this class to confirm the answers to your original question.

  • RAISERROR with Try/Catch does not exit after exception in catch block

    I am trying to propogate an error from within my proc out to the caller.
    In the attached example I have 2 sets of try catch blocks.
    I raiserror in the first
    catch the error and then raiserror again. (I expect to exit)
    I do not expect :
    to get to print 'post test'
    to get to second try block.
    but this does not exit, instead the code flows as per 2 runs.
    I do not understand the reason for the flows, as it seems counterintuitive to be raising an error but then still print following exceptions. I cannot seem to find any references that explains this behaviour.
     running tests together results
    print '-------------------------------------------------------'
    print 'test 15'
    exec test_raiseerror 15
    print '-------------------------------------------------------'
    print 'test 16'
    exec test_raiseerror 16
    print '-------------------------------------------------------'
    print 'test 17'
    exec test_raiseerror 17
    print '-------------------------------------------------------'
    print 'test 18'
    exec test_raiseerror 18
    print '-------------------------------------------------------'
    'RESULTS'
    test 15
    error number provided: 15
    Msg 50000, Level 15, State 1, Procedure test_raiseerror, Line 21
    name hello 15
    post test
    15
    Msg 50000, Level 15, State 1, Procedure test_raiseerror, Line 37
    name hello 2 15
    post test2
    test 16
    error number provided: 16
    Msg 50000, Level 16, State 1, Procedure test_raiseerror, Line 21
    name hello 16
    post test
    16
    Msg 50000, Level 16, State 1, Procedure test_raiseerror, Line 37
    name hello 2 16
    post test2
    test 17
    error number provided: 17
    post test
    17
    post test2
    test 18
    error number provided: 18
    post test
    18
    post test2
    Msg 50000, Level 17, State 1, Procedure test_raiseerror, Line 21
    name hello 17
    Msg 50000, Level 17, State 1, Procedure test_raiseerror, Line 37
    name hello 2 17
    Msg 50000, Level 18, State 1, Procedure test_raiseerror, Line 21
    name hello 18
    Msg 50000, Level 18, State 1, Procedure test_raiseerror, Line 37
    name hello 2 18
    run tests seperately
    exec test_raiseerror 15
    error number provided: 15
    RESULTS 15
    Msg 50000, Level 15, State 1, Procedure test_raiseerror, Line 21
    name hello 15
    post test
    15
    Msg 50000, Level 15, State 1, Procedure test_raiseerror, Line 37
    name hello 2 15
    post test2
    exec test_raiseerror 16
    RESULTS 16
    error number provided: 16
    Msg 50000, Level 16, State 1, Procedure test_raiseerror, Line 21
    name hello 16
    post test
    16
    Msg 50000, Level 16, State 1, Procedure test_raiseerror, Line 37
    name hello 2 16
    post test2
    exec test_raiseerror 17
    RESULTS 17
    error number provided: 17
    post test
    17
    post test2
    Msg 50000, Level 17, State 1, Procedure test_raiseerror, Line 21
    name hello 17
    Msg 50000, Level 17, State 1, Procedure test_raiseerror, Line 37
    name hello 2 17
    exec test_raiseerror 18
    RESULTS 18
    error number provided: 18
    post test
    18
    post test2
    Msg 50000, Level 18, State 1, Procedure test_raiseerror, Line 21
    name hello 18
    Msg 50000, Level 18, State 1, Procedure test_raiseerror, Line 37
    name hello 2 18
     CODEBLOCK:
    if object_id('test_raiseerror','P') is not null
    drop proc test_raiseerror
    go
    create proc test_raiseerror(@id as int) as
    begin
    begin try
    declare @name varchar(20)
    select @name = 'hello'
    raiserror('name %s %d',@id,1,@name,@id)
    print 'next'
    end try
    begin catch
    declare @errormessage nvarchar(4000)
    declare @errornum int
    select @errormessage = error_message()
    , @errornum = error_severity()
    print 'error number provided: ' + convert(varchar(2),@errornum)
    raiserror(@errormessage, @errornum,1)
    print 'post test'
    end catch
    begin try
    select @name = 'hello 2'
    raiserror('name %s %d', @id,1,@name, @id)
    end try
    begin catch
    select @errormessage = error_message()
    , @errornum = error_severity()
    print @errornum
    raiserror(@errormessage, @errornum,1)
    print 'post test2'
    end catch
    end
    go
    sqlserver 2008 & 2008 R2

    There is a Connect that describes a similiar complaint.  But basically a raiserror inside a catch block does not terminate the procedure, it will continue with any additional code in the CATCH and FINALLY unless it hits a return statement.
    http://connect.microsoft.com/SQLServer/feedback/details/275308/have-raiserror-work-with-xact-abort

  • Exception handling with try/catch in acrobat

    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?

    > 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

  • How to accept 2 strings in a class with try catch method..help!!

    the program below will accept two strings and compare str1 and str2 if equal. this program uses functions. can any one help me with this?
    import java.io.*;
    public class StrCompare {
         private BufferedReader takyoin = null;
         //private BufferedReader intakyo = null;
         * @param args
         public StrCompare(){
              takyoin = new BufferedReader(new InputStreamReader(System.in));
              //intakyo = new BufferedReader(new InputStreamReader(System.in));
         public String UserInput(){
         public static void main(String[] args) {
              // TODO Auto-generated method stub
    }

    What are you talking about? There is no such thing as "try-catch-methods", and there are no functions but methods.
    Strings by the way have their own means of comparision. Apart from that: do your own homework.

  • Is it possible try  with out catch but with finally

    can any body tell me that try with out catch but with finally is possible or not and also if I dont want to execute the finally block but i want to execute the try and catch then what can i do for that.

    ... and also if I dont want to
    execute the finally block but i want to execute the
    try and catch then what can i do for that.Obviously the best way then would be to not have the finally block at all.
    But if you insist then add a boolean flag. Set it in the catch and test it in the finally block.

  • Surround With Try/Catch : comments issue

    hi
    Please consider this code:
         public static void main(String[] pArguments)
              FileInputStream vFileInputStream = new FileInputStream("someFile.txt");
              // some comments, e.g. several lines of commented code (that might be uncommented later)
         }It is possible to have JDeveloper "Surround With Try/Catch" this, but that results in this code:
         public static void main(String[] pArguments)
              FileInputStream vFileInputStream;
              // some comments, e.g. several lines of commented code (that might be uncommented later)
              try
                   vFileInputStream = new FileInputStream("someFile.txt");
              catch (FileNotFoundException e)
                   // TODO
         }Note that the declaration is before the comments and the try block starts after the comments.
    Is this intentional behaviour? I would prefer to leave the comments where they are, after the (modified) code.
    many thanks
    Jan Vervecken

    Thanks for your reply John.
    I tried the right-click "Surround With..." > "try-catch" approach you suggest, and I get this code:
         public static void main(String[] pArguments)
              try
                   FileInputStream vFileInputStream = new FileInputStream("someFile.txt");
              catch (FileNotFoundException e)
              // some comments, e.g. several lines of commented code (that might be uncommented later)
    }Indeed, the try-block starts before the comments.
    There is another difference with this approach, the declaration is not separated from the assignment.
    Next ... I'll try to remember which approach give which result. :)
    regards
    Jan

  • Problem with a Try/catch exception

    Hello everyone here on the forums. I'm brand new to Java and have a bit of a problem with a try catch statement I'm making. In the following code if a user enters a non-integer number the program will display "Sorry, incompatible data." Problem is it gets caught in a loop and continues to display "Sorry, incompatible data." My aim with the try catch was if the user is not quite smart enough to understand quadratic programs don't use symbols and characters it would state it isn't correct and continue running the program.
    Heres my code thus far:
    package finishedquadraticprogram;
    import java.util.*;
    * @author Matt
    public class quad {
         * @param args the command line arguments
        public static void main(String[] args) {
            boolean verification = true;
            double a, b, c, root1, root2, discriminant;
            Scanner keyInput = new Scanner(System.in);
            while ( verification)
            try
            System.out.println("a: ");
            a = keyInput.nextDouble();
            System.out.println("b: ");
            b = keyInput.nextDouble();
            System.out.println("c: ");
            c = keyInput.nextDouble();
            discriminant = Math.sqrt(b * b - 4 * a * c);
            root1 = (-b + discriminant) / 2 * a;
            root2 = (-b - discriminant) / 2 * a;
            verification = false;
            System.out.println("Root 1 = " +root1+ "\nRoot 2 = " +root2);
            } //try
            catch  (InputMismatchException iMe)
              System.out.println( "Sorry. incompatible data." );  
    }I'm pretty sure the problem is something to do with the keyboard buffer, but I'm not sure what I need to do to either reset it or clear it, whichever one works. (Oh, and how would I go about making the program use complex numbers? I realize Java can't use complex numbers and will just display NaN ... any ideas how to make this work?)
    Thanks a lot for all of your help guys, it's appreciated.

    this is better:
    package finishedquadraticprogram;
    import java.util.*;
    /** @author Matt */
    public class quad
       private static final double TOLERANCE = 1.0e-9;
       /** @param args the command line arguments */
       public static void main(String[] args)
          boolean verification = true;
          double a, b, c, root1, root2, discriminant;
          Scanner keyInput = new Scanner(System.in);
          while (verification)
             try
                System.out.println("a: ");
                a = keyInput.nextDouble();
                System.out.println("b: ");
                b = keyInput.nextDouble();
                System.out.println("c: ");
                c = keyInput.nextDouble();
                discriminant = Math.sqrt(b * b - 4 * a * c);
                if (Math.abs(a) < TOLERANCE)
                   root1 = 0.0;
                   root2 = -c/b;
                else
                   root1 = (-b + discriminant) / (2 * a);
                   root2 = (-b - discriminant) / (2 * a);
                verification = false;
                System.out.println("Root 1 = " + root1 + "\nRoot 2 = " + root2);
             } //try
             catch (InputMismatchException iMe)
                System.out.println("Sorry. incompatible data.");
                keyInput.next();
    }

  • Difference: "throws xyException" and "try/catch"

    Is there a difference? Which is it? Would thus be the same for a function:
    "functionName throws xyException"
    or
    "functionName{
    try{
    catch(xyException){
    }

    functionName throws xyException
    Calling code must call this function in a try and catch block or code won't compile.
    functionName{
    try{
    catch(xyException){
    Calling code does not have to put the call to this function in a try and catch block.
    public void methodName() throws xEcxeption{
              try{
                        // something
              }catch(Exception e){
                        // try to clean up something
                        // trhow xEcxeption to calling code so that code can do something as well.
                        // like trying an alternative, informing the user or stop executing
    }

  • TRY CATCH with dynamic ORDER BY

    Some stored procs need a dynamic ORDER BY, unfortunately.
    Can I use a TRY CATCH with ORDER BY?
    SELECT ...
    FROM ...
    WHERE...
    BEGIN TRY
    ORDER BY ...complex stuff
    END TRY
    BEGIN CATCH
    ORDER BY ColumnA
    END CATCH

    Hi mmmtbig,
    Syntactically, it is ok to use any valid sql statement in the TRY AND CATCH block.
    SELECT 1 as num
    BEGIN TRY
    SELECT 1/0 AS seq UNION SELECT 2 ORDER BY seq;
    END TRY
    BEGIN CATCH
    PRINT ERROR_MESSAGE();
    SELECT 1 AS seq UNION SELECT 2 ORDER BY seq;
    END CATCH
    For more details, You can reference
    TRY...CATCH.
    The confusion about your question is the "dynamic ORDER BY". What do you mean by that, can you elaborate your requirement, like in what scenario you'd like to use the code block in your post and what issue have stopped you from doing that?
    If you have any feedback on our support, please click
    here.
    Eric Zhang
    TechNet Community Support

  • Weird thing with try-catch

    Hey everyone, I have a problem compiling this .java
    import java.io.*;
    public class lector
        public static void main (String args[])
        public void lector()
            listapersonas listap = new listapersonas();
            listaconocidos listac = new listaconocidos();
            String linea, per, con;
            persona nuevapersona,actual,nuevoconocido;
            BufferedReader texto = new BufferedReader(new FileReader("conocidos.txt"));
            linea="";
            try {
            while ((linea=texto.readLine())!=null)
                per = linea.substring(0,linea.indexOf(','));
                con = linea.substring(linea.indexOf(','));
                nuevapersona = new persona(per);
                nuevoconocido = new persona(con);
                if (listap.existe(per)==false)
                    nuevapersona.conocidos.agregar(nuevoconocido);
                    listap.agregar(nuevapersona);
                    linea=texto.readLine();
                    continue;
                if (listap.existe(per)==true)
                    if (listac.existe(con)==false)
                        listac.agregar(nuevoconocido);
                    else
                        linea=texto.readLine();
                        continue;
                else
                    linea=texto.readLine();
                    continue;
            }  //while
            } //try
            catch (java.io.* e)
                System.out.print("Imposible leer el archivo");
            } //catch
        } //lector
    } // classWell what it is supposed to do is read a string from a file and then transform and add them to a linked list.
    The problem is that when I compile there is an error message called "<identifier> expected" and points to the catch block. Please help me

    Nothing weird about it at all.
    That's not the syntax of a catch block. What you've written there will only work in an import statement.
    What you probably mean to catch is java.io.IOException.

  • How to check a lot of conversion errors with one TRY-CATCH?

    Hi,
    I've to import a lot of data from a flatfile.
    To avoid short dumps when there are non-numeric data's for a numeric field in the file, I load every record in a structure that have only CHAR fields.
    Then I move this fields in a TRY-Catch block to the real fields like this:
    TRY.
        field1 = field_from_file1
        field2 = field_from_file2
        field3 = field_from_file3
    CATCH cx_sy_conversion_no_number INTO lr_exception.
        PERFORM fehler USING lr_exception.
    ENDTRY.
    This construct works until the first field has a conversion error. However I will test all fields to get a complete list of the errors. And I am to lazy to make a TRY-CATCH construct for hundred of move-statements.
    Is there a better trick to check the success of <b>all</b> move operation?
    Regards,
    Stefan

    Hi Stefan,
    sorry, the development system is down right now, so I could not test. But I'm convinced you can figure it out:
    You may specify the parameters of a method as TYPE REF TO DATA.
    Then, in the calling routine you may have
    data:
      anyrefsource type ref to data,
      anyreftarget type ref to data.
    GET REFERENCE OF p_source INTO anyrefsource.
    GET REFERENCE OF p_target INTO anyreftarget.
    call method try_conv
      exporting
        source = anyrefsource
        target = anyreftarget.
    and in the method:
    ASSIGN source->* TO <fs_source>
    and so on...
    BTW: I know you have to use typed parameters in OO. Can't you use "TYPE ANY"?
    regards,
    C.

Maybe you are looking for

  • Why do I get a popup message in multiple languages requiring a shutdown and restart

    Why do I keep getting a popup message in multiple languages requiring a shutdown and restart?  Has been going on for a few months.  Because of the shutdown reports have been sent to Apple upon restart.

  • IDOC sender Principal Propagation

    Hi experts, I've a scenario IDOC to JDBC, it give me a error. I could have seen in others threads this error can be relationed with 'Principal Propagation' but i don' understand this concept, also in this scenario i haven't a sender agreement (becaus

  • Testing links

    I post these links for the assistance of those Windows users who may have troubles with the Akamai Download Manager. If you can use the downloader do so - it has resume capabilities. If you need to use the direct links below note that they work in Fi

  • Password not accepted by appstore

    I wonder why all apps which demand Aplle ID and password like iCloud, Apple support ect. work well on the same imac, but only AppStore DOES NOT accept it (message "password not correct"). I also reset my password - but no result. All works fine, exce

  • Condition Groups in Contracts

    Hi, Is it possible to default few condition types in contracts transaction types in CRM under the Sales Agreeement tab? The issue is that, there is a condition group assiged to the transaction type instead of pricing procedure. Can some one help me t