try { for (UserRecord UR : list) { if (name.equalsIgnoreCase(UR.userName)) { if (UR.handler == null) { UR.handler = clienthandler; return; //use return to exit this block } else { throw new InterruptedException(); } } } throw new InterruptedException(); } catch (InterruptedException e) { clienthandler.sendMessage("System : invalid user!"); }
异常处理中,异常的情况使用throw抛出异常
正常的情况要使用return,否则始终都会执行到异常