Exceptions in Java

What is an exception?

  • Exception is the event that disturbs the normal flow of execution of program.
  • The runtime system is handed over the error by the object(Exception object) created within the method.

Exception object contains type and state of program where error occurred.

  • Runtime system searches for the callstack(called because the methods are called in reverse manner )that contains exception handler(block of codes) to handle exceptions.
  • The termination of program occurs if the required exception handler is not found and program run exhaustively. Java Terminology for the above process is called throwing an exception.

The exception handling  involves some blocks such as try, catch and finally block.

  • The first step of enclosing the exceptions are done by try block.
  • Catch block associates the try block by being present in more in numbers which enables handling the more than one exception in java present in the program. Even after java SE7 onwards only single catch block is capable to handle more than one which in order reduces the code duplication.
  • The finally block is executed when try block exits .It cleans up resources , paticurlary in context of IO
  • A keyword is often termed with this concept is throws keyword. This keyword enables the method which generates unhandled exception to throw it.

Syntax:

try {
      //code

        } catch(Exception e1) {


 
                //handle exception e1

           }

            }


 
            finally{

                //code executed after try and catch block

            }

Comparision with traditional programming

Java has  upper hand over traditional programming language because of use exceptions :-

m1{

try{


 
call m2;

}catch(exception){

doErrorProcessing


 
}}

m2 throws exception{

call m3;


 
}

m3 throws exception{

call readFile;


 
}
  • In separating error handling code from regular code  as java uses catching and handling mechanism to handle the exceptions .
  • The try block catches the error and method that specifies which can throw it.
  • If the code fails to satisfy the catch and specify requirement the code won’t compile where as in case of pseudo-programming ,it has a function to read the entire file .
  • This process of execution of tradional programming comes up with certain limitations such as if the file can’t be opened ,whether length determines or not ,whether memory has enough space or not .
  • Error propagation up the call stack method  has a prior advantage as java runtime system searches backward. It is only about the methods that considers the errors have to worry about detecting error.

The above pseudocode requires a middleman to skip the it. Thus it shows that which contains exceptions to make the program compact.

  • Another advantage contains group errors type and differentiation where often exceptions fall under categories. For example we have Arrayexception subclass of Exception class which further divides into three more subclasses such as invalidIndexException, ElementtypeException and NoSuchException. InvalidException groups are those which shows that index specified doesn’t exist. Similarly other two forms group.

GeeksGod
Logo
Shopping cart