10: Exception Handling



Compilation Error:
Ø  During compilation time compiler will check for the syntax errors of the program (grammatical errors).
Ø  Again it will check for proper environment is for JVM to execute byte code or not.
Ø  Syntax errors and improper environment errors leads to compilation error.
Logical Error:
Ø  Even if the code is compiled by compiler JVM cannot execute the byte code generated by compiler and JVM will generate an error, which is known as logical error.
Ø  For each and every logical error JVM will create one object that objects will reach back to JVM, immediately will terminate the entire program without executing.
Ø  Developers(JAVA soft people) think that if we can stop the object created by the
JVM without reaching back to JVM, then we can stop the termination of entire program, because of one logical error.
Exception Handling:
Ø  The concept of stopping the object created by the JVM, because of a logical error, without reaching back to JVM is known as exception handling.
Ø  To do this we will take one variable from corresponding class object and assigned the object created by the JVM into that variable, now variable will hold the object without reaching back to JVM.
Program:

Explanation: In the above program the statement y=x/0, we are trying to initialize infinity value into integer variable y, but JVM cannot understand how it can store infinity value into integer variable, this is an ‘abnormal condition’ or logical error.
                   So JVM will create one class object and that object will reach back to JVM, now JVM will terminate the entire program.









If we use try, catch block to handle the exception, try block will hold the exception created by the JVM and transfer it into catch block, catch block will store that variable into corresponding class object.
After handle the exception of above program is given below.
public class Demo {
Result:
         

          public static void main (String [] args)     {
                   int x=100;
                   try     {
                   int y=x/0;
                   System.out.println(y);
                   }    catch (ArithmeticException ae){       }
                   System.out.println("Hello World!");
                   System.out.println ("1");         }      }
Program for handle arithmetic exception:




































Result:
func1 of class A
before exceptiopn
k val is 0
a
         



when we provide the try, catch block if there is a logical error, JVM will not terminate the entire program because of logical error, it will just terminate code in a try block after the logical error.
Ø There are two kinds of logical errors.
1. Simple logical errors and
2. Serious logical errors.
Simple logical errors:
Ø There are some errors, which can be neglected by the JVM, these errors are known as simple logical errors or exceptions.
Ø All sub classes of exception class are known as simple logical errors or exceptions.

Serious logical errors:
Ø Some logical errors cannot be neglected by the JVM, these are known as serious logical errors or errors.
Ø All sub classes of error class are known as serious logical errors or errors.
Hirariechy of Exception handling:






Explanation: This approach is not recommended to use if many statements are proven to generate different exceptions, because of developers cannot remember which statement is proven to generate and which exception class is required, in such situations we will provide only one try-catch block with super class exception variable, which can handle all the exceptions inside the block because exception class is a super class of all the exception classes and super class reference variable can be use to refer all sub class variables.
Example:






Checked Exceptions: compiler will compels us during compilation about some exceptions, these exceptions are known as checked exceptions.
Examples:
Ø FileNotFoundException
Ø ClassNotFoundExceptions
Ø IOException
Unchecked Exceptions: some exceptions recognized by JVM during runtime, which are known as unchecked exceptions.
Examples:
Ø ArithmeticException
Ø NullPointerException
Ø IndexOutBoundsException
Ø ArrayIndexOutBoundsException
“throws” keyword:
Ø Throws is a keyword, which is used to declare the exception, which is not handled as part of the function.
Ø If we are using throws keyword to declare an exception to the function, the exception has to be handled from its calling place
Program






Result:
            After executed
Note:
1. If there is an exception inside the try block in one line, statements before that line will be executed, but after that line will not be executed, but remember statements outside the try block will executed irrespective of the exception.
class A   {
    void function1 () throws NullPointerException   {     }
    class B extends A   {
    void function2 () throws NullPointerException       //Exception
      }      //compilation error

Explanation: suppose there is method, where the statements inside the method proven to generate a null pointer exception
   While writing the body to that method normally we have to handle the exceptions, otherwise we can transfer the exception class object created in the method through calling place of method using throws keyword.
Note: If there is an exception inside a function either we have to handle or we have to declare function using throws keyword.
Program:
              





finally:
Ø finally is a keyword in java to support the exception handling.
Ø finally block will be executed irrespective of the exception.
Ø That means suppose there is an exception inside try block, corresponding catch block and finally block will be executed.
Ø If there is no exception inside try block, finally block only executed.
Program:



Result:
          Catch block
            Finally block


Note: we can use the concept of exception handling to constructor also.


Custom Exceptions:
Ø We can write our own exception classes, which are known as custom exceptions.
Ø To do this we have to extend our class java.long.exception and provide three constructors, which are shown in below example.



Example:






Difference between throws and throw:
Ans: No, it will give compilation error.
Q. can we have a try block without catch block?.
Ans: Yes, but try block should be finally block atleast.
Try{
        }finally{
        }
     
But this is recommended to use in coding, because we cannot come to know at which the JVM has terminated the program if there is exception, if there is no exception we don’t need try block.
 





No comments:

Post a Comment