JAVA | Define Exception? How is it handled?

Exception: An exception is an event, which occurs during the execution of a program, that stop the flow of the program's instructions and takes appropriate actions if handled. .i.e. It is erroneous situation encounter during course of execution of program.

Exceptional handling mechanism provides a means to detect errors and throw exceptions, and then to catch exceptions by taking appropriate actions. 

Java Exception handles as follow 
 Find the problem (Hit the exception)
 Inform that an error has occurred ( throw the Exception)
 Receive the error information(Catch the exception)
 Take corrective action ( Handle the Exception) 

Exception handling in java is done by 5 keywords as:
try: This block applies a monitor on the statements written inside it. If  there exist any exception, the control is transferred to catch or finally block. 
catch: This block includes the actions to be taken if a particular exception occurs.
finally: finally block includes the statements which are to be executed in any case, in case the exception is raised or not. 
throw: This keyword is generally used in case of user defined exception, to forcefully raise the exception and take the required action.  
throws: throws keyword can be used along with the method definition to name the list of exceptions which are likely to happen during the execution of that method.  In that case, try … catch block is not
necessary in the code. 

Post a Comment

0 Comments