Tuesday, 14 July 2015

EXCEPTION HANDLING IN JAVA



Exception handling in java :
While developing the java program there are some chances that the code which we have written can’t be executed then such type of situation is known as exception. Here we have two types of exceptions
Caught exception (checked exception)
Unchecked exception (UN caught exception)
Caught exception:
While developing the code if java itself is giving the hint that there may be an exception then such type of exceptions is known as called exception.
Un Caught exception:
During the execution if java can’t do something then such type of exceptions are known as un called exception.
Try {  } … catch { }:
Whenever we are developing a program if at all we have a doubt in any block of program which may lead to exception then it is better to keep that block in try and if exception occurs then what should happen we will write in catch block













public class ex {

         
          public static void main(String[] args) {
                   try {
                             int a[]=new int[3];
                             System.out.println("still trying...");
                             a[0]=10;
                             a[1]=20;
                             a[5]=30;
                             System.out.println(a[0]);
                   } catch (Throwable t) {
                             System.out.println(t.getMessage());
                             t.printStackTrace();
                             }
                  
                   finally { System.out.println("!oops i(finally) executed ");}
                   }



          }












Demonstration of try catch
Finally { }:
The script present in the finally block will be executed at any cost irrespective of try block or catch execution is successful or not.
Throws:
Throws class is used for automatically throwing the exception whenever we don’t want something specially to happen whenever exception occurs then instead of using try catch block it is better to use throws .here the main reason is throws class can handle the same exception any no of times in the program.
Throwable:
It is a supper class of both Exception and Error class for catching an exception or error.
 we can simply write “ Throwable t ” in catch block instead of writing exception or error
Eg: catch (Throwable t) {
          System.out.println(t.getMessage());
          t.printStackTrace();
}
Throw:
It is used for throwing an error explicitly by the program.
Used to throw user defined messages if error occurs




Demonstration of throw class
package pack1;
public class throwdemo {
                   public static void main(String[] args) {
                   String expval="sree kanth",actval="sree";
                   if(expval.equals(actval)) {
                             System.out.println("passed");
                   }
                   else {
                             throw new Error("not working fine");
                   }
          }
}


Ternary operator:  It is an alternative for if else.
Syntax: condition ? trueresult : falseresult ;

public class exception {

         
          public static void main(String[] args){
                          String av="sree",ev="sree";
          System.out.println(ev.equals(av) ? "passed" :"failed");

          }
}

No comments:

Post a Comment