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 found 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)
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):
Disadvantage is also clear: ugly exception handling construction.
But in Java you can't avoid it. It's language rules.