今天就跟大家聊聊有關(guān)在Java項(xiàng)目中如何跳出遞歸循環(huán),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
使用異常跳出循環(huán)
1、如果方法體內(nèi)含有需要拋出異常的對象,讓方法直接拋出異常,不要在方法體內(nèi)捕獲
public void xxxx() throws Exception
2、如果方法體內(nèi)不含有需要拋出異常的對象
class Test { static class StopMsgException extends RuntimeException { } public static void main(String args[]) { try { run(0); } catch (StopMsgException e) { System.out.println(e); } } public static void run(int t) { if (t > 20) { // 跳出 throw new StopMsgException(); } // 執(zhí)行操作 System.out.println(t); // 遞歸 run(t + 1); } }
看完上述內(nèi)容,你們對在Java項(xiàng)目中如何跳出遞歸循環(huán)有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。