Java: how to have try-catch as conditional in for-loop?

Posted by HH on Stack Overflow See other posts from Stack Overflow or by HH
Published on 2010-04-27T23:59:08Z Indexed on 2010/04/28 0:03 UTC
Read the original article Hit count: 277

Filed under:
|
|
|

I know how to solve the problem by comparing size to an upper bound but I want a conditional that look for an exception. If an exception occur in conditinal, I want to exit.

import java.io.*;
import java.util.*;

public class listTest{
        public static void main(String[] args){

                Stack<Integer> numbs=new Stack<Integer>();
                numbs.push(1);
                numbs.push(2);
                for(int count=0,j=0;try{((j=numbs.pop())<999)}catch(Exception e){break;}&&
                                !numbs.isEmpty(); ){
                                System.out.println(j);
                }
                // I waited for 1 to be printed, not 2.

        }
}

Some Errors

javac listTest.java
listTest.java:10: illegal start of expression
        for(int count=0,j=0;try{((j=numbs.pop())<999)}catch(Exception e){break;}&&
                            ^
listTest.java:10: illegal start of expression
        for(int count=0,j=0;try{((j=numbs.pop())<999)}catch(Exception e){break;}&&
                               ^

© Stack Overflow or respective owner

Related posts about for-loop

Related posts about java