10.5 JAVA SYNTAX FOR EXCEPTION HANDLING


10.5 JAVA SYNTAX FOR EXCEPTION HANDLING

Here is the general syntax for exception handling in Java:

     try {         .....         .....     } catch(exception_type_1 identifier_1) {         .....         .....     } catch(exception_type_2 identifier_2) {         .....         .....     } catch(exception_type_3 identifier_3) {         .....         .....     } finally {         .....         .....     } 

where .... stands for statements. If all the statements in the body of the try block are executed successfully, the flow of control shifts directly from the try block to the finally block. However, as soon as an exception occurs during the execution of any of the try statements, the type of the exception is compared with the types declared for the parameter of each of the catch blocks in the order of appearance of the catch blocks. When a match is found, the statements in that catch block are executed. No further catch blocks are examined. After the end of processing in the invoked catch block, the flow of control shifts to the finally block if one is included. Therefore, the code in the finally clause is executed whether or not an exception is thrown. The finally clause is useful for affecting processing that must be carried out even when an exception is thrown. For example, suppose you open an input stream to a local file from which you read data incrementally, meaning one item at a time. Let's say an exception is thrown during this process, either because the data item could not be read because it was of the wrong kind, or for whatever other reason, you'd still want to close the input stream. The finally clause comes handy for that, as we will see in the next section.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net