Java: How would I write a try-catch-repeat block?

后端 未结 4 2045
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-14 20:00

I am aware of a counter approach to do this. I was wondering if there is a nice and compact way to do this.

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-14 20:48

    Legend - your answer could be improved upon; because if you fail numTries times, you swallow the exception. Much better:

    while (true) {
      try {
        //
        break;
      } catch (Exception e ) {
        if (--numTries == 0) throw e;
      }
    }
    

提交回复
热议问题