exception handling can be managed by five keywords.
1. Try: The try block governs the statements that are enclosed within it and defines the scope of exception handler associated with it. Try block follows catch or finally or both. 2. Catch: This is a default exception handler, since the exception class is the base class for all the exception classses, this handler is capable of catching and type of exception.
The catch statement takes an object of exception class as a parameter, if an exception is thrown the statement in the catch block is executed. The catch block is restricted to the statements in the proceeding try block only.
//statements that may cause exception
catch (Exception e)
3. Finally: When an exception is raised, the statement in the try block is ignored, sometimes it is necessary to process certain statements irrespective of whether an exception is raised or not, the finally block is used for this purpose.
4. Throw: The throw class is used to call exception explicitly. You want to throw an exception when the user enters a wrong login ID and password, you can use the throw statement to do so. The throw statement takes an single argument, which is an object of exception class.
If the object does not belong to a valid exception class, the compiler gives error.
5. Throws: The throws statement specifies the list of exception classes that are thrown by a method. If a method is capable of raising an exception that is does not handled, it must specify the exception that has to be handled the calling method, this is done by using the throw statement.