Suppose I have 10 lines of code. Any line maybe throw a NullPointerException. I don’t want to care about these exceptions. If a line throws the exceptions, I want the executor jump to next line and go forward. Could I do this on Java? if yes, please give me some sample code.
UPDATE: (add sample source for more clear question)
first.setText(prize.first);
second.setText(prize.second);
third.setText(prize.third);
fourth.setText(prize.fourth);
fifth.setText(prize.fifth);
sixth.setText(prize.sixth);
seventh.setText(prize.seventh);
eighth.setText(prize.eighth);
Suppose I have 8 lines of code above. What I want is: if the line 4 (or 5, 6,…) throws an exception, all other lines of code works normally. Of course I can use try…catch to catch the exceptions line by line. But this way make my source very complex.
The fact that a variable is null is notable, and should not be ignored. Therefore, you’d want something like this:
If, for some reason, you absolutely need to throw NPEs, you would need to surround each line with its own try/catch; there’s no way to automatically catch exceptions and have it advance to the next line. It’s not, however, recommended to do this.