Continuing with exception..!!




  • Now as we learnt about how exception works internally how throw of object occurs in exception well as said earlier that java has provided a readymade class for handling of exception.
  •  java provided a readymade class such as ArithmaticException,ArrayIndexOutfBoundsException, ClassCasteException, NumberFormatException,etc.
  • Due to such ready made classes we can write its object in catch block to catch exception
  • if for example the exception of divide by zero occurs  then declare the object of ArithmeticException in catch statement such as 
  • try
  • {
  •     c=a/b;
  • System.out.println("the answer of division is" + c);
  • }//end of try block 
  • catch(ArithmaticException e)
  • {
  •    System.out.println("exception caught" + e);
  • } //end of catch block
This is the simple exemple which i gave............. here if the exception occurs than the object of that exception is generated and thrown and in catch as we kept the object of that exception it will be caught it is not necessary that only a type of exception will be thrown how ever there can be possibilities such that a number of exception would be thrown so can declare a multiple catch statement for multiple exception to be thrown and the exception goes to the suitable catch.

OR    the second way is to just the declare the object of exception class which is the base of all the claases of exception that single object would work for multiple exception

e.g.catch(Exception e)

  • Now we could wonder that how it works well for the ready made exception the object is thrown implicitly but throw keyword is not useful here if we want to explicitly throw back the exception than throw keyword is useful now let me give u an example for it in ready made classes
  • as in above example the object was thrown implicitly and caught in the catch block we didnt wrote the keyword throw there but now if we want to write then..........
  • well the exception came for ArithmaticException so we wrote the catch statement such as 
    
         catch(Exception e)
        {
           System.out.println("exception caught " + e);

           throw e;
         }

see here again an exception is thrown but its meaningless to throw such an exception back after catching then where it is usefull u might have a question about it

  • well there is a place for it it is used when we create our exception class yes we can create our own exception class known as CustomException class this we can be made by inherting the base class that is the Exception class .

  • example 
         class divException extends Exception//runtimeException
         {
            string kushal, dhanraj;

            divException(string e)
            {
                 Super(e);
                 kushal = exception caught;
            }
                
Yes this was the example of custom exception any normal class when extended with the exception class becomes an exception class 

so when the talk comes about exception let me explain you with example

  1.   we input two values such as for int a and int b now we have to do a/b so definetly if b is 0 t       then  exception is going to be generated but as we want to use our exception the coding we       might think will work as 

                                             try
                                            {
                                                  if(b= = 0)
                                                  {
                                                     throw  new divException( );//nameless object
                                                  }

                                                 else 
                                                 {
                                                   c= a\b;
                                                   System.out.println("division is " + c);}
                                            }//end of try block

                                           catch(divException e)
                                          {
                                           System.out.println( e );

                                           }//end of catch block

How could it work ...????

 when exception occurs we made the object of our class thrown explicitly and caught that object as we threw that object we can write any message we want to display in it can pass when we create an object of that class as we have inherited that exception class with divException its constructor will be automatically called and can pass message by writng explicitly super otherwise it will call its base default constructor.

normally all ready made class performs this action only by calling its base constructor because how to throw an object and what all procedure is to be performed where an object is to be thrown is written in the class exception in this class that is arithematic exception is used to initlialize the message and pass to its base or if exception is not generated how to divide in the given expresion is written in it so now i hope the concept would be cleared.....!!!!!!!!!!!



now the concept of the keyword throws


throws keyword is used when we the method or function itself says that it would not be able to handle this type of exception the exception could be any....

well to make it more understandable method itself says that in future i will throw this type of exception and plzz handle it so it says by writing the keyword throws besides it and gives us a command to catch that exception later on in the program

The working is such that by writing the throws keyword besides the declaretion of function we have to write the exception name which is to be thrown so now as we are restricted that we have to catch that exception we have to write the calling statement using the try catch block

lets take it practically

                        class abc
                         {
                          int a ,b,c;
                          Public static void main(String args[ ])
                          {
                           public static div( ) throws ArithmaticException
                               {
                                   c=a/b;
                                }

//as by using the keyword throws we are allowed to write its calling statement in try catch block now going further.....
                            try
                           {
                              div( )// we can call as method is static its within the same class so called directly                                                so it will behave as an udf
                           }//end of try block

                          catch(ArithmaticException e)
                          {
                              System.out.println(e);
                           }//end of catch block

                  }//end of main

}//end of class

working of above program

  • if the exception is generated the contrl will come to where it is called but as we wrote its calling statement in the try block it will definitely find its corresponding catch further in the program so by catching an exception the rest statement after try catch would be executed and the normal end by executing all the statements will occur.


This was all about the concepts of exception later many other related topic would be covered by me such as checked and unchecked exception ,hierarchy of classes of exception etc..
hope u might have enjoyed reading this article....                        


                                                           
                          
        


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