Java Exception handling

# Exception handling:

Exception handling is the process of responding to the occurrence, during computation, of exceptions – anomalous or exceptional conditions requiring special processing – often changing the normal flow of program execution.

Where we use Exception??

 ..... 
         int a=20;
         int b=0;
         int c=a/b;
         System.out.println(c);
                                        ..............

..................
                 String s=null;
                 System.out.println(s.length());
                                                       .......................


  • The Mathematical will never allow the value dividing by zero, For such a situation Exception is used to throws the error
  •  The String is set as null, here NullPointerException will resolved by Exception handling.
Example:
public class Exception_Example
  {
     public static void main(String args[])
        {
            int a=20;
            int b=0;
               try
                  {
                      int c=a/b;
                      System.out.println(c);
                    } catch(ArithmeticException error)
                          {
                              System.out.println("Exception:" +error);
                           }
               System.out.println("Rest of the code will be executed");
            }
    }

Output:


Exception:java.lang.ArithmeticException: / by zero
Rest of the code

 

How does it works??  
sathishkumar blogspot - Exceptional Handling

No comments:

Post a Comment