Java: how to have try-catch as conditional in for-loop?
- by HH
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;}&&
^