//Add a throws keyword after the parameter list of the method and list the classes for the //exceptions that might be thrown separated by comma { }
Throw , Try and Finally Block
If we need to handle the exception where they occur we use the try, catch and finally blocks. Try block encloses the code that may give rise to one or more exceptions. Code that can throw an exception that you want to catch must be in a try block, Catch block contains the code that is handling the exception of a particular type that may be thrown in a try block. Finally block contains code that is always executed before the method ends regardless of whether any exceptions are thrown in the try block.
try { //code that can throw exceptions } { //code to handle the exception } finally { // code to be executed last }
try { // Code that can throw exceptions of type ArithmeticException and IndexOutOfBoundException } // Code to handle exception of either type... }
{ throw e; } //The keyword throw is followed by the exception object to be thrown