开发者

is that possible in java? (code to execute) if (condition)

开发者 https://www.devze.com 2023-03-16 11:58 出处:网络
Please note that I am not a java guru. I might not use the right terminology, I learn java inside RFT on the fly. What is described below works exactly as stated.

Please note that I am not a java guru. I might not use the right terminology, I learn java inside RFT on the fly. What is described below works exactly as stated.


In ruby we can code like

(code to execute) if (condition)

I want to do the same so my RFT (Rational Functional Tester) code is easy to read. I am going to call my custom functions in a way that looks like

        findANDclick(new String[]{"id", "menuButton"});
        findANDclick(new String[]{"src", ".*homeicon_calendar.*"}); 
        findANDclick(new String[]{"src", ".*cycle_templates.*"});   

But the whole RFT script needs to finish and don't execute any other code in case of any of the findANDclick functions 'failed'. The function searches for an object with in html page and if it doesn't find any it throws new Exception via

throw new Exception("findANDclick: the object was not found");

so the instance of findANDclick ONLY throws an error so the next findANDclick is executed. But it makes no sense to continue as look for next object if the previous was not f开发者_运维问答ound and clicked on.

I was thinking that I can have a variable continue set to true and in case the exception is thrown the findANDclick will update it to false. Then I can do something like

    if (continue) { findANDclick(new String[]{"id", "menuButton"});}
    if (continue) { findANDclick(new String[]{"src", ".*homeicon_calendar.*"}); }   
    if (continue) { findANDclick(new String[]{"src", ".*cycle_templates.*"});   }

it would be great if I can do something like

        { findANDclick(new String[]{"id", "menuButton"}); } if (continue)
        { findANDclick(new String[]{"src", ".*homeicon_calendar.*"}); } if (continue)
        { findANDclick(new String[]{"src", ".*cycle_templates.*"}); } if (continue)


That code is not pretty. Why not simply catch the Exception? That is how they are meant to be used. So something like:

try { 
   findANDclick(new String[]{"id", "menuButton"});
   findANDclick(new String[]{"src", ".*homeicon_calendar.*"});    
   findANDclick(new String[]{"src", ".*cycle_templates.*"});   
}
catch (Exception e) {
}

If any of the findANDclick calls fail, the subsequent ones will not be executed...

Btw, continue is a reserved word in Java, so you cannot use it as a variable name.


No, you cannot do this.

However, if the method throws an exception, why do you still need to check the continue flag then? It will stop anyways if the findANDclick fails.


Radek! Try to execute the following example, just to clarify how the Java exception handling works.

public class ExceptionHandling {
    /*Method that throws exception*/
    static void methodOne() throws Exception {
        System.out.println("methodOne();");
        try {
            throw new Exception();
        } catch (Exception e) {
            System.out.println("Exception caught in methodOne(), worked up, and thrown again.");
            throw new Exception();
        }
    }

    static void methodTwo() {
        System.out.println("methodTwo();");
    }

    public static void main(String[] args) {
        try {
            methodOne();
            methodTwo();
        } catch (Exception ex) {
            System.out.println("Exception caught in main()!");
        }
    }
}

Output of this example:

methodOne();
Exception caught in methodOne(), worked up, and thrown again.
Exception caught in main()!

It shows that the second method is never executed, if the first one throws an exception.

P.S. This should be a comment. But it's clearer in rich formatting.

Solution suggested by Mathias Schwarz

try { 
   findANDclick(new String[]{"id", "menuButton"});
   findANDclick(new String[]{"src", ".*homeicon_calendar.*"});    
   findANDclick(new String[]{"src", ".*cycle_templates.*"});   
} catch (Exception e) {
   // Workup exception somehow.
}

has advantages to which you aspire (from Java developer's point of view):

  1. It's short;
  2. It's clear for Java developer.

Disadvantage is also clear: ugly exception handling construction.

But in Java you can't avoid it. It's language rules.


You can't use if. You can use a do/while loop that more or less provides the syntax you are looking for.

do { findANDclick(new String[]{"id", "menuButton"}); } while (continue)
do { findANDclick(new String[]{"src", ".*homeicon_calendar.*"}); } while (continue)
do { findANDclick(new String[]{"src", ".*cycle_templates.*"}); } while (continue)

BTW, like someone mentioned above, continue is a bad choice for a variable name.


I think you should try this:

    String[][] params = {
        {"id", "menuButton"},
        {"src", ".*homeicon_calendar.*"},
        {"src", ".*cycle_templates.*"}
    };

    for (String[] param : params) {
        if ( findANDclick(param) ) break;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消