2
try{
  if(condition A || condition B || condition C)
    do something
}catch (IndexOutOfBoundsException e){
  //Equivalent command to continue
}

Suppose I have an exception in condition B and I want it to ignore it and check if condition C is fulfilled. How can I do that if it is not a loop structure and therefore it cannot be used the continue command?

3
  • Throw your custom exception on condition B, inside your catch check for condition C Commented Dec 9, 2014 at 14:58
  • The problem is that it is in B as it can be in A. And if the list of conditions was much greater, it would not be quite productive to include one catch for each of them. Commented Dec 9, 2014 at 15:00
  • See docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html Commented Dec 9, 2014 at 15:06

4 Answers 4

1

You could do something like this.

Assuming you do the same operation on all of your conditions of them. (T is your data type)

    List<T> conditions = ArrayList<>();
    boolean flag = false;
    for(T condition : conditions) {
        try{
            if(!condition)
                flag = false;
        }catch (IndexOutOfBoundsException e){
            //Equivalent command to continue
        }
        if(flag) {
            //do something
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

usually we use condition when we want to check something, not change it.

if one of the condition didn't pass, then there is no reason to continue, or else it wouldn't be a condition.

now there are several suggestion i can give you, but probably the best one will be to initiate the values of the conditions before the try block, and the if after:

boolean A = false;
boolean B = false;
boolean C = false;
try{
  A = condition A;// this will probably be a function
  B = condition B;
  C = condition C;

}catch (IndexOutOfBoundsException e){
  //Equivalent command to continue
}

if(condition A || condition B || condition C)
  do something

now, we can expend it to 3 try blocks, or even a function like so:

private boolean conditionA()
{
   boolean A = false;

   try{
      A = condition A;

    }catch (IndexOutOfBoundsException e){
      //Equivalent command to continue
    }

    return A;
}

Comments

1

Using Guava Predicate:

Predicate<MyInput> a = new Predicate(){
    @Override
    public boolean apply(MyInput i) {
        return condition A;
    }
};
Predicate<MyInput> b = new Predicate(){
    @Override
    public boolean apply(MyInput i) {
        try {
            return condition B;
        } catch (IndexOutOfBoundsException e) {
            return false;
        }
    }
};
Predicate<MyInput> c = ...

if(Predicates.or(a, b, c).apply(myInput)) {
    // do something
}

Comments

0

The continue keyword would not help: you cannot continue evaluating an expression after encountering an exception. If you would like to treat an exception in a condition in the same way as a condition that evaluates to false, wrap the condition in a method, and call that method instead:

boolean methodForConditionB() {
    try {
        return ... // Actual condition goes here
    } catch (IndexOutOfBoundsException e) {
        return false;
    }
}
...
if (conditionA || methodForConditionB() || conditionC) {
    ...
}

This can be used as a work-around. Generally, however, you should avoid using exceptions for transferring control during regular computations, i.e. outside of exceptional situations. Specifically, IndexOutOfBoundsException signals a programming error, so it is hard to imagine circumstances when catching it would be good design.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.