Why exception handling is important?.It is because if something unwanted behavior happens in your code should not stop the normal execution and stop immediately.

In day to day life when we use safety gloves when working with electricity it is meant for protecting us if the current is there in the equipment we are handling the rubber gloves will protect us from shock.

The types of exceptions are 

  1. Compile time exception.
  2. Run Time exception.

Compile time exception occurs at compile time it means in very simple words if you are writing some code and compiling the code.It will show you the error.

example-You are trying to open a file that does not exist

Run Time exceptions are the one those occur at run time. These error won’t come in picture until you run your code.

example-If you have array of size 3 and you try to access the item located at size 5 then you won’t the item at all because you are accessing the location which never exist.

Object

|

Throwable

|————————|

Error                 Exception

What is try?

     This is block where you place your code which may throw exception.

What is catch?

     This is the block where exceptions are handled.just like when we feel sick we go to doctor and doctor handles our sickness and even makes note of it what went wrong.

What is finally?

     We need finally block to handle the things irrespective of whether exception occurred or not.Suppose you got the database connection related error in the try block and now your connection is open the code in catch block got executed due to error.So risk is it might create memory leak.To avoid this we simply keep statements 

which should be executed every time in finally block.

     

package
com.exceptionhandling;

public class
ExceptionHirarchyDemo {

       public static void main(String[] args) {

              try{

                     System.out.println(“”+(8/0));

              }catch(ArithmeticException ex)

              {

                     System.out.println(“Arithmatic
Exception”
+ex.getMessage() );

              }catch(Exception ex)

              {

                     System.out.println(“Exception is
+ex.getMessage() );

              }

       }

}

package
com.exceptionhandling;

public class
ExceptionHirarchyUnReachableDemo {

       public static void main(String[] args) {

              try{

                     System.out.println(“”+(8/0));

              }catch(Exception ex)

              {

                     System.out.println(“Exception is
+ex.getMessage() );

              }

       /*     catch(ArithmeticException ex)

              {

                     System.out.println(“Arithmatic
Exception”+ex.getMessage() );

              }*/

//At Line 13:Unreachable catch block
for ArithmeticException.

//            It
is already handled by the catch block for Exception

       }

}

package
com.exceptionhandling;

public class HelloException {

public static void main(String[] args)

{

System.out.println(“”+(1/0));

//This exception divide by zero will
never execute below line

//and abnormaly exits the
whole program.

System.out.println(“Hello
World”
);

//instead we will try to catch this
exception print it and also execite rest of the line

//Please see HelloException2

}

}

package
com.exceptionhandling;

public class HelloException2 {

       public static void main(String[] args)

       {

              try{

                     System.out.println(“”+(1/0));

              }catch(ArithmeticException e)

              {

                     System.out.println(“”+e.getMessage());

              }

              //This exception
divide by zero handled now so Hellow World

              //is executed

              System.out.println(“I will be
executed as exception is handled now Hello World”
);

             

       }

}

package
com.exceptionhandling;

public class KnowFinally {

       public static void main(String[] args) {

              int myValue=5;

       try{

              myValue=6;

              int result=myValue/0;

              System.out.println(“”+myValue);

             

              }

              catch (Exception e) {

                     System.out.println(“I visited
Catch”
);

                     myValue=7;

              }

              finally {

                     System.out.println(“I visited
Finally”
);

                     myValue=10;

              }

System.out.println(“Final Result
+myValue);

       }

}

/**OUTPUT

 * I visited Catch

  
I visited Finally

  
Final Result 10

 *

 */

package
com.exceptionhandling;

public class KnowReturn {

static void print(int number)

{System.out.println(“”+number);

       }

       public static void main(String[] args) {

              int myValue;

              try{

                     myValue=6;

                    

              //     System.out.println(“”+myValue);

                     return;

                     }

                     catch (Exception e) {

                           System.out.println(“I visited
Catch”
);

                           myValue=7;

                     }

                     finally {

                           myValue=10;

                           System.out.println(“I visited
Finally”
);

                           print(myValue);                         

                     }

       }

}

/**

 * Output

 * I visited Finally

       10

 */




Leave a Reply

Your email address will not be published. Required fields are marked *