The example shows how to monitor and catch an exception.
Trying to access an index outside the bounds of an array generates an ArrayIndexOutOfBoundsException.
The program intentionally causes this exception and catches it.
The code to be monitored for exceptions is placed inside a try block.
When an exception occurs, it is thrown and caught by the catch block, ending the try block.
Control is not "called" to catch, but is transferred automatically.
If no exception occurs, the catch block will be ignored and the program continues normally after the try block.
Exceptions raised by methods within the try block can also be caught by catch, if the method does not handle the exception on its own.
The exception generated by genException() is caught by the catch block in the main() method, as it was called within a try block.
If genException() had caught the exception, it would not have been passed to main().
The above is the detailed content of Simple Exception Example. For more information, please follow other related articles on the PHP Chinese website!