When to use exceptions in Java (example)

前端 未结 10 1908
既然无缘
既然无缘 2020-12-06 17:05

I know that this would be bad practice although I know that I would not be able to explain why.

int [] intArr = ...
...
try{
   int i = 0;
   while(true){
         


        
10条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 17:42

    Reaching the end of an array is not an exceptional case. You know the length before you start looping, so just use a standard idiom.

    for(int i = 0; i < intArr.length; ++i) {
        System.out.println(intArr[i]);
    } 
    

提交回复
热议问题