把try{}catch(Exception e){}放for循环里面啊,for要做的事情放try{}里啊:
boolean flag=true;
for(int i=0;i<10;i++){
try{
//...
}catch(Exception e){
//System.out.println(FALSE);
flag=false;
//continue;
}
}
return flag;
//不知道能否满足要求,你试下吧
//循环继续的指令是continue,循环终止跳出用break;
boolean flag=true;
for(int i=0;i<10;i++){
try{
//...
}catch(Exception e){
//System.out.println(FALSE);
flag=false;
//continue;
}
}
return flag;
//不知道能否满足要求,你试下吧
//循环继续的指令是continue,循环终止跳出用break;
把代码贴出来研究一下怎么写吧。
public static void main(String[] args) {
int i = 0;
String[] s = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11" };
System.out.println(loop(i, s)); } public static boolean loop(int i, String[] s) {
try {
for (int iSize = s.length; i < iSize; i++) {
if (i == 5) {
throw new Exception(i + ":这里抛出异常!");
}
System.out.println(i + ":" + s[i]);
}
} catch (Exception e) {
loop(i + 1, s);
return false;
}
return true;
}
循环应该就不会继续了,整个方法就退出了
如果要让循环继续,catch块里面用continue;
try--catch块放在循环里面
public static void main(String[] args) { method(); } public static void method() {
for (int i = 0; i < 10; i++) {
try { int k = 10 / i;
System.out.println(k); } catch (Exception e) {
e.printStackTrace();
continue;
}
}
}
但无论是否有异常,循环都需要执行完,是这个意思不?
for(xxxx){
methoda();
}
在methoda()里我做了try/catch,那如果异常了,循环继续否?
public static void main(String[] args) {
int i = 0;
String[] s = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11" };
System.out.println(loop(s)); } public static boolean loop(String[] s) {
boolean a = true;
for (int i = 0; i < s.length; i++) {
try {
if (i == 5) {
throw new Exception(i + ":这里抛出异常!");
}
System.out.println(i + ":" + s[i]);
} catch (Exception e) {
a=false;
continue;
}
}
return a;
}
结合楼上二位,呵呵。lisong526的有递归,gjsong的没有返回值。
但是如果你是在methoda里throw得异常for循环也会终止的