Yes.
try and catch Work
Here is how
tryandcatchwork:
- When an
Exceptionis thrown by a statement in thetryblock, thecatchblocks are examined one-by-one starting starting with the first.- The first
catchblock to match the type of theExceptiongets control.
- In the diagram, X, Y, and Z represent different classes of exceptions.
- For example, Y might be
IOException.- A
catchblock matches exceptions of the named class and all subclasses- If Y is
IOException, it also matchesFileNotFoundException, a subclass.- Only one
catchblock gets control.- If no
catchblock matches theException, none is picked, and execution leaves this method (just as if there were notryblock.)- The most specific
Exceptionclasses should appear first in the structure, followed by the more generalExceptionclasses.
- If the general class came first, it would be the first match for all its subclasses.
- The statements in the chosen
catchblock execute sequentially. After the last statement executes, control goes to the first statement that follows thetry/catchstructure.
- (A
finallyblock can be added to the structure, which will always be executed.)- (This explained below.)
- Control does not return to the
tryblock.
Must the catch blocks list all possible Exceptions?