Sunday, April 7, 2013

Exception Handling

Exceptions are objects created by JVM that represent the corresponding logical errors.

Syntax errors: Syntax errors occur whenever a statement written by the programmer cannot be compiled by the compiler.

Whenever a source code is compiled, first compiler checks whether a proper environment is available for the JVM or not to execute the byte code which it has generated for the source code.
Thus, Syntax errors and improper environment result in compilation errors.

Logical Errors: Sometimes, JVM would be unable to execute statement(s) in the byte code for some reasons. This results in logical errors.

Note  : Whenever a logical errors, program is terminated and control comes out of the program unconditionally without mentioning from which part of the program it has come out.

Ex: Class A
{
Public static void main(String args[])
{
int i =100;
int j = 0;
int x= i/j;
system.out.println(x);
}
}

-          The above program will be perfectly compiled. But, while executing it, value of x becomes “infinity” and size x is of type integer(32 bit) JVM would not understand how to store infinity value in 32 bit location.

-          Thus are some predefined classes in java.lang.package whose objects are created by JVM whenever it encounters a logical error. (Names of such classes are found under the exception classes in API).

-          For each and every logical error, there is a corresponding exception class in the java.lang.package.

-          Whenever JVM encounters a logical error, it creates an object of the corresponding exception class and since this object is not explicitly handled, this object reaches back to the JVM. JVM(a set of programs) accepts these objects and terminates the program.

-          Now, if we are able to handle those exception class objects, we can prevent the program from getting terminated.

-          Thus, concept of handling the exception class object and avoiding them from reaching back to the JVM known as Exception Handling.

-          We can handle the exception class objects by assigning them to the corresponding exception class refrences.

Procedure of handling the exceptions:

-          We have two key words ‘try’ & ‘catch’ to implement exception handling.

-          The ‘try’ keyword recognizes the exception class object created by the JVM and it gets hold of these objects and transfers them to the “catch” block.

-          In the “catch” block we should be able to assign those objects to the appropriate refrences so that they will not reach JVM and therefore program would not be terminated.

Ex:
------
function1()
{
try{
---
}
catch(Exception e1)
{
system.out.println(“Exception in fun1()”);
system.out.println(e1);
}
---
}

-          Thus, as soon as an exception is raised the user would easily know in which part of the program the logical error has occurred.

-          system.out.println(e1) prints some message instead of printing the address contained in e1.This is because the exception class overrides the tostring method of the object class.

-          In this way, we can use try and catch blocks in any part of the program.

Uses of Exception Handling:

-          We can know in which part of the program the logical error has actually occurred.

-          We are not allowing the JVM to terminate the entire program. Only the ‘try’ block in which the error has accrued would be terminated.

-          Thus, this is an easy way of debugging the program.
Note :  
 i) Once control centres the catch block. It executes the statements in the catch block and moves on further. It will not come back to the ‘try’ blocks because of which the ‘catch’ block was executed.

ii) If no exception class object is created in the ‘try’ block, control will never enter the catch block.

iii) Every ‘try’ block should be associated with at least one ‘catch’ block.

iv) A ‘try’ block can have any no .of ‘catch’ blocks.

Interview Q. Can we define a try block without a catch block?

Ans) Yes, but that procedure is not recommended.

We have altogether five keywords in the concept of exceptions. They are
i)                    try
ii)                   catch
iii)                 throw
iv)                 throws
v)                  finally

Logically errors are of two types.
i)                    Simple logical error
ii)                   Serious logical errors

Simple logical errors: There are some logical errors which can be neglected by the JVM.such errors are known as simple logical errors.

Ex: int x = 100/0; etc...

Serious logical errors : Some logical errors cannot be neglected by the JVM. Such errors are known as serious logical errors.

Ex: Creation of an object of a class. When these is no availability of memory space in the Ram.
All the classes coming under ‘exceptions’ are defined to represent ‘simple logical errors’.
All the classes coming under ‘errors’ are defined to represent ‘serious logical errors’.