How can I exit a method if it returns nothing?

Например: void method(array[][]){ if(array==null) завершить работу \* рабочий код *\ } 
  • return; probably so. - maestro
  • This code is similar to the pre-conditions check. Accordingly, it is possible and not necessary to complete, but it is better to throw out, for example, IllegalArgumentException ... After all, if you think about it, then somewhere in the program we call this void method and then we don’t know if it worked or was completed ahead of time. Maybe there are such tasks, but it seems to me that this is initially the wrong approach. - Olexiy Morenets

1 answer 1

Add a return;

 void method(int[][] array) { if(array == null) { return; } else ... } } 

Or simply:

 void method(int[][] array) { if(array == null) return; ... } 

Ps. ar r ay.

  • In the first variant, return not needed, you can leave an empty if branch, and then it is better to invert the condition and leave only one branch. - Roman
  • @Roman, The question was posed very precisely: “How to urgently complete the void method?”, I answered exactly that, and “how to organize the work of the method more correctly” is another story. - post_zeew
  • Then why do you even bring the first option? From the point of view of the question , the first option duplicates the second. - Roman
  • @Roman, In order to show that it can be done this way (but this does not mean at all that this is the best option). - post_zeew