Prévia do material em texto
131 (B) Define exceptions? An exception is an abnormal condition that arises in a code sequence at run time. (B) Can you explain in short how JAVA exception handling works? Basically there are four important keywords which form the main pillars of exception handling: try, catch, throw and finally. Code which you want to monitor for exception is contained in the try block. If any exception occurs in the try block its sent to the catch block which can handle this error in a more rational manner. To throw an exception manually you need to call use the throw keyword. If you want to put any clean up code use the finally block. The finally block is executed irrespective if there is an error or not. Below is a small error code snippet which describes how the flow of error goes. Figure 1.52: - JAVA exception in action In the try block we have two exceptions raised one (1/0) which will throw an exception error and the other will use the throw clause to throw a general exception error. Arithmetic exception will flow from try block to the arithmetic catch block and then execute the finally block. But the general exception will flow from the try block, then to the exception