Exception handling allows program continuity:
Example of exception being caught:
Code example:
class ExcDemo3 { public static void main(String args[]) { int numer[] = { 4, 8, 16, 32, 64, 128 }; int denom[] = { 2, 0, 4, 4, 0, 8 }; for (int i = 0; i < numer.length; i++) { try { // Tenta realizar a divisão System.out.println(numer[i] + " / " + denom[i] + " is " + numer[i] / denom[i]); } catch (ArithmeticException exc) { // Captura e trata a exceção de divisão por zero System.out.println("Can't divide by Zero!"); } } } }
Program output:
4 / 2 is 2 Can't divide by Zero! 16 / 4 is 4 32 / 4 is 8 Can't divide by Zero! 128 / 8 is 16
Exceptions are removed after being handled:
Benefit:
Exception handling allows the program to handle repeated errors and continue its execution flow smoothly.
Conclusion:
Exception handling allows the program to continue running by handling errors such as division by zero, rather than terminating execution.
The above is the detailed content of Exceptions allow you to handle errors normally. For more information, please follow other related articles on the PHP Chinese website!