Wednesday 28 August 2013

Exception Handling

Basically exceptions are also known as the run time error.which terminate the flow of  program .these types of exceptions are handled by the exception handling.The main advantage of exception handling is that it maintains the normal flow of program.

Two types of exceptions:-
Checked Exceptions:-The exceptions that are checked by the java compiler are called checked exceptions.
in case of checked exception compiler is looking for either throws or try and catch.without this we cannot compile the program.

Example:-class not found , SQL exception , IO exception , file not found , clone not supported , parse exception , interrupted exception.

Unchecked Exceptions:-The exceptions that are checked by the JVM are called the unchecked exceptions.in case of unchecked exception the compiler doesn't look for try and catch it will compile the program successfully.

Example:- Arithmetic exception , null pointer exception , number format exception , Array index out of bound , class cast exception , stack overflow , memory out of bound , no class definition found , assertion error.

5 keywords used for handle exceptions
try , catch , finally , throw , throws.

Example-not use try and catch to handle exceptions.



Example :-handle above exception using try and catch



try:-insert the code in try block in which exceptions may occurs.
catch:-insert the code to handle the exception and also declare the exception name.
if exceptions occurs then catch block will execute .if error doesn't come then catch block will not execute.
SOP(ex):-prints the exception message.

Example;-if exception occurs in catch block...



Example:-handle exception of catch block by using multiple try block



it handles the both exception occurs in try and catch block.

finally:-finally block is optional.we should place finally block after all catch block.
if the cursor enters into the try block then finally block must execute no matter exceptions occurs or not.
we cannot put any statement between:-
  1. try and catches.
  2. between catches.
  3. catches and finally.
we can use only one finally block in the program and it should be the last block.
Example:-


Example:-when finally block will not executes.the cursor will not enter in the try block so finally not executes.



Example:-as discussed above that in unchecked exceptions compiler will not be looking for try and catch.
it compiles the program successfully. it shows run time error.


Example:-Checked exception without try and catch. so as discussed above that in checked exception compiler will be looking for try and catch block .if try and catch will not present in the program it will give compile time error.




Example:-Checked exception with try and catch block.compiler doesn't give any compile time error.





Example:-Single try and multiple catch.



single try with multiple catch.in catch we define different types of exception. which type of exception occurs corresponding catch will  executes.
At a time only one catch will be executed.

NOTE:-All catches must be in the order of most specific to most general means first you write the sub class then go for super class. if you write super class first then it handles the exception and the sub classes after the super class will not be executed.so it will provide unreachable catch error.

Throw:-it is used to throw an exception explicitly.it is used to make a custom exception .if you want to make your exception and you want to throw it at particular conditions .so for achieve this by using throw keyword.

Example:-


if we enter the i value below 10 then it will throw exception with the message "below 10".

Throws:-it is used to declare an exception.it indicates that exception may occurs in the code so it helps programmer to handle that error and maintained the normal flow of program.

Example:-



in this first it declares the error in print() method which indicates that error may occurs .so used proper catch to handle this error.

Exception Handling with Method Overriding:-

Case 1:-Exception is not declared by the Super class.
if the exception is not declared by the super class then the sub class overridden method  can not go for checked exception but go for unchecked exception.

Case 2:-Exception is declared by the Super class.
if the exception is declared by the super class then  the sub class overridden method can declare same exception or not declare any exception but it can not go for super class exception.






No comments :

Post a Comment