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 is 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().
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3