How does exception actually works and the mechanism behind it ..In JAVA

Working of exception

      what is exception...??

  • simple  meaning of exception is run time error the error which occurs while execution the program.For example how this exception are generated is by the user inputs the false value or its the fault of the programmer having no better concepts about it.

    Some of the examples of exception are ArrayIndexOutOfBoundsException,  NumberFormatException,NullPointerException,ClassCasteException, Arithmetic Exception.

    so above were the exceptions that could be generated not limited to this still there are many but this all are the basic which would occur normally.

    Common scenarios where exceptions may occur

    There are given some scenarios where unchecked exceptions can occur. They are as follows:

    1) Scenario where ArithmeticException occurs

    If we divide any number by zero, there occurs an ArithmeticException.

    1. int a=50/0;//ArithmeticException  


    2) Scenario where NullPointerException occurs

    If we have null value in any variable, performing any operation by the variable occurs an NullPointerException.

    1. String s=null;  

    2. System.out.println(s.length());//NullPointerException  


    3) Scenario where NumberFormatException occurs

    The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur NumberFormatException.

    1. String s="abc";  

    2. int i=Integer.parseInt(s);//NumberFormatException  


    4) Scenario where ArrayIndexOutOfBoundsException occurs

    If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as shown below:

    1. int a[]=new int[5];  

    2. a[10]=50//ArrayIndexOutOfBoundsException 

    One thing to note we cant say that exception will surely occur in our program it may or may not occur in program.

    so the best concept are provided in java if u want to learn exception.

    Java provided ready made class for handling exception.

    First  of all exception and exception handling are the two different thing what is exception i explained at first now exception handling.

    Java Exception Handling Keywords

    There are 5 keywords used in java exception handling.

  • try
  • catch
  • finally
  • throw
  • throws 

  •  Now let us assume that an exception is generated in between the program than happens is that the control goes directly from the line of exception to the logical end that is the abnormal end so what i mean to say is that it would not execute the statements after the exception one.
     
  • The core advantage of exception handling is to maintain the normal flow of the application. Exception normally disrupts the normal flow of the application that is why we use exception handling. Let's take a scenario:
    • statement 1;  
    • statement 2;  
    • statement 3;  
    • statement 4;  
    • statement 5;//exception occurs  
    • statement 6;  
    • statement 7;  
    • statement 8;  
    • statement 9;  
    • statement 10;  
     
  • so see here the exception is generated at the 5 Th line  so the rest of the statement will not be executed and brings the abnormal end of the program which is not good as many useful statements, if are written than may not be executed if exception comes.
  • Now how they are used will see later on first of all what happens if we do not take steps to handle exception....!!
  • Actually if exception occurs then the statement written after it are not executed firstly and control goes to the normal end and the error comes where the user has input the values that is the screen or the editor if in cpp has the editor and in java we has command prompt. that is from where the user inputs the values 
  • so some steps should be taken to control the exception so as written earlier these 5 words are useful to control the exception.

Java try-catch


Java try block

Java try block is used to enclose the code that might throw an exception. It must be used within the method.
Java try block must be followed by either catch or finally block.

Syntax of java try-catch

  1. try{  
  2. //code that may throw exception  
  3. }catch(Exception_class_Name ref){}
  • Now what is try and catch block try block is the block in which we write the doubted code that is in which we have possibility to get exception now as we write try and in try we write the doubted code so as we declare try we need to declare catch with it that is we had to catch the exception so if we declare the try block we need to declare the catch block and if not we get an error .
  • Now by declaring try we are saying that we have written the mechanism to catch the exception later in the program that is the catch block.
  • so as soon as we get the exception in try block it will try to find the suitable catch block immediately
  • As exception is generated an object corresponding to it is generated having the information about the type of exception and is thrown automatically by the compiler if we have written the code in try block it will find its suitable catch and if not the exception will be thrown on the screen.
  • Here in catch block works as the function see if you are doubted that your code will throw arithmetic exception than declare the object of arithmetic exception in corresponding catch statement.
  • Syntax of try-finally block

    1. try{  
    2. //code that may throw exception  
    3. }finally{} 
     
  • finally stand for to execute its statements for sure if the exception is caught then the finally block occurs and its statements are read and then the rest statement are executed  if an exception doesn't occur than also the finally statements are executed .
  • so finally block will execute for sure.

See the block diagram of how the try catch works 

internal working of try-catch block
try catch block working




rest part is in the seperate post as it is most important part of it










































 


 

Comments

Popular posts from this blog

State space search / blocks world problem by heuristic approach /TIC TAC TOE

Navigation in Vaadin.

Drag and drop items from one Grid to another