java - Continue the execution after exception -
i have these 2 class :
public class tryexception { int a=0; tryexception(int c) { = c; } public boolean operation() //just example { if(a!=10) { system.out.println(a); return true; }else{ throw new runtimeexception("display something"); } } }
and main :
public class test { static public void main(string args[]) { int val =20; tryexception ex = new tryexception(val); try{ while(ex.operation()){ ex.a = --val; } }catch(runtimeexception e) { system.out.println("try exception"); } } }
when run program, execution stoped when detects exception. how continue execution of same while
after exception ?
move try-catch inside loop.
boolean run = true; while(run){ ex.a = --val; try{ run = ex.operation(); }catch(runtimeexception e){ system.out.println("try exception"); } }
you need decide when set run
false
...
Comments
Post a Comment