When to use exceptions in Java (example)

前端 未结 10 1864
既然无缘
既然无缘 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:49

    Exceptions are for exceptions in the code. Not for standard cases.

    however, there are one more serious issue with your code, it is slower the it would be without the use of exception. Creating the exception, throwing the exception and catching the exception takes additional CPU and MEMORY.

    Also the code gets harder to read for other programmers that only expect Exceptions to be thrown on error cases.

提交回复
热议问题