Saturday, March 29, 2014

Java Exception




අද පාඩමෙන් මං ඔයාලට කියල දෙන්න බලාපොරොතු වෙන්නේ ජාවා වල තියන Exception කියන එක. Exception කියන එක ගැන සරලව කිවොත් මේවා තමයි run time errors.run time errors කියන්නේ ප්‍රෝග්‍රෑම් එක compile වෙනවා අව්ලක් නැතිව නමුත් run කරනකොට Exception එකක් තියනවා කියල කියනවා.හරියටම කිවොත් මෙන්න මේ වගේ වැඩක් තමයි වෙන්නේ. පහත දැක්වෙන ප්‍රොග්‍රෑම් එක බලන්න.

public class Exception {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            int x=0,d=0;
            System.out.println("A");
            d=42/x; //Runtime Error
            System.out.println("B "+d);

      }

}
මේකේ output එක එන්නේ මේ වගේ.

Exception in thread "main" java.lang.ArithmeticException: / by zero
A
      at Exception.main(Exception.java:11)

Aකියාල ප්‍රින්ට් වෙලා ඊටපස්සේ තමයි error එකක් දෙන්නේ.මේ ප්‍රොග්‍රෑම් එක කිසි අව්ලක් නැතිව compile වෙනවා.නමුත් error එකක් එන්නේ මේ ප්‍රොග්‍රෑම් එකේ තියන ගණිතමය වරදක් නිසා.0/0 කියන්නේ අර්ථ විරහිත අවස්ථාවක්.මේ වගේ වෙලාවල් වලට ජාවා වලින් Exception දෙනවා.අපි තව බලමු Exception දෙන ප්‍රොග්‍රෑම් කිහිපයක්.

class Exception{
      public static void main(String args[]){
            int[] array=new int[5];
            int a=5;
            System.out.println("A");;//line1
            array[a]=200;//Runtime Error
            System.out.println("B "+array[a]);
      }
}
අපි සාමාන්යෙන් දන්නවා array එකකට එලිමෙන්ට් 5 තියනවනම් index තියන්නේ 4 වෙනකම් කියල.නමුත් මේ ප්‍රෝග්‍රෑම් එකේ 5 index එකට තමයි 200 කියන අගය දන්න හදන්නේ.නමුත් එහෙම index එකක් නෑ.නමුත් මේක compiler ට හඳුනාගන්න බෑ.එක නිසා පහත Exception එක දෙනවා.

A
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
      at Exception.main(Exception.java:6)

හොඳට බලන්න output එක.එකේ A කියල ප්‍රින්ට් වෙලා ඊට පස්සේ පේලිය රන් වෙන්න යනකොට තමයි Exception එකක් ඇවිල්ල තියන්නේ.

මේ විදියට අපිට ප්‍රදාන වශෙන් හමුවෙන Exception වර්ග කිහිපයක් තියනවා.ඒවගෙන් කිහිපයක් තමයි පහත දැක්වෙන්නේ.



·         ArrayIndexOutOfBoundsException

·         IndexOutOfBoundsException   

·         Null Pointer Exception  

·         RuntimeException යන ඒවා.
දැන් මේ වගේ Exception එකක් ප්‍රෝග්‍රෑම් එකක අවොත් ප්‍රොග්‍රෑම් එක ඒ Exception එක එන තැනින් එහාට රන් වෙන්නේ නෑ.මේ නිසා අපිට ප්‍රොග්‍රෑම් එකේ අනිකුත් කොටස් වලින් වෙන්නේ මොකක්ද කියල හොයාගන්න බැරි වෙනවා.මේ අඩුපාඩුව මගහරවාගැනීම Handling Runtime errors තියනවා.

අපි මුල්ලින්ම බලමු මුලින්ම ගත්තු ප්‍රොග්‍රෑම් එකේ එන ගණිතමය වරද තියාගෙන run time errors නැතිව රන් කරන්නේ ඛෝමද කියල.

public class Error {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            int x=0,d=0;
            System.out.println("A");

            try{
                  d=42/x;
            }catch(ArithmeticException ob){
                  d=-1;
                  ob.printStackTrace();
            }
            System.out.println("B "+d);


      }

}
බලන්න මේකේ ගණිත කර්මය කරලා තියෙන්නේ try –catch block එකක් ඇතුලේ. සරලවම කිවොත් අපි try එක ඇතුලේ දන්නේ අපිට  run time error ගෙනදෙන code එක.catch එක ඇතුලේ දන්නේ try එක රුන් වෙන්න බැරි වූවොත් වෙන්න ඕනේ දේ තමයි. catch(ArithmeticException ob)කියල pass කරන්නේ Exception එකේ වර්ගයට අදාල object එකක්.අපිට ඕනනම් ob.printStackTrace(); කියන මෙතොඩ් එකින් අදාල error එක මොකක්ද කියල ප්‍රින්ට් කරලා බලාගන්න පලුවන්.



අපි දැන් 2 වන ප්‍රොග්‍රෑම් එකටත් try catch එකක් දල බලමු.

public class Error {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            int[] array=new int[5];
            int a=5;
            System.out.println("A");
           
            try{
                  array[a]=200;
            }catch(ArrayIndexOutOfBoundsException e){
                e.printStackTrace();
            }
            System.out.println("B ");


      }

}

දැන් ඔයාලට පෙන්නවා ඇති try එක ඇතුලේ තියන error එන code එක රන් වෙන්නේ නැතිව ප්‍රොග්‍රෑම් එකේ අනිත් ටික රන් වෙනවා කියල.එකේ output එක එන්නේ මෙන්න මේ වගේ.

A
java.lang.ArrayIndexOutOfBoundsException: 5
      at Error.main(Error.java:14)
B

දැන් ඔයාලට ඇතිවෙන ප්‍රශනය තමයි catch block එකට දාන Exception එකේ නම මොකක්ද කියල.Exception කියන්නේ ජාවා වල කලින් define කරලා තියන class එකක්.අපිට API එකින් ගියාම බලාගන්න පලුවන්.කලින් පෝස්ට් එකකදී මං කියල දුන්න API එක බලන විදිය.



class Exception කියන එකින් ඉන්හෙරිට් වෙච්ච error එකට අදාල ඕනෙම එකක් දන්න පලුවන්.ඒ වගේම අපිට ඒ class එකේ ඕනෑම super class එකකකුත් දන්න පලුවන්.ඒ කිවේ අපි හිතමු error එක Array Index Out Of Bounds Exception කියල එක ඉන්හෙරිට් වෙලා තියන්නේ Exception කියන class එකින් එක නිසා ඒ වනුවට Exception කියල දැම්මත් වරදක් නෑ.

public class Error {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            int x=0,d=0;
            System.out.println("A");
            try{
                  d=42/x; //x=0
            }catch(ClassCastException ob){
                  System.out.println("catch block..");
                  ob.printStackTrace();
            }
            System.out.println("B ");


      }

}
Error එකට අදාල නැති එකක් catch එක ඇතුලේ දැම්මොත් අයෙත් class එක රන් වෙන එක නවතිනවා.එතොකොට error මගහරින්න බැරි වෙනවා.

class A{
      void mA(){
            System.out.println("mA 1");
            int d=42/0;
            System.out.println("mA 2");
      }
}
class B{
      void mB(){
            System.out.println("mB 1");
            new A().mA();
            System.out.println("mB 2");        
      }
}
public class Error {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            B b1=new B();
            System.out.println("main 1");
            b1.mB();
            System.out.println("main 2");
      }

}

Output එක.

main 1
mB 1
mA 1
Exception in thread "main" java.lang.ArithmeticException: / by zero
      at A.mA(Error.java:4)
      at B.mB(Error.java:11)
      at Error.main(Error.java:24)

මේ ප්‍රෝග්‍රෑම් එකේ class A වලින් තමයි error එක එන්නේ.එක නිසා try –catch block එක දන්න ඕන class A වලට එතකොට error එකක් නෑ.class එක මේ වගේ වෙනස් කරානම් හරි.

class A{
      void mA(){
            System.out.println("mA 1");
            try {
                  int d=42/0;
            } catch (ArithmeticException e) {
                  // TODO: handle exception
                  e.printStackTrace();
            }
            System.out.println("mA 2");
      }
}

Multi Catches
අපිට පලුවන් catch block කිහිපයක් පවචිකරන්නත්.පහත ප්‍රෝග්‍රෑම් එක බලන්න.

public class Error {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            int a=5,b=0;
            int[] ar=new int[5];
            String name="Kasun";
           
            System.out.println("A");
            try{
                  ar[a]=(int)name.charAt(6)/b;
            }catch(Exception e){
                  e.printStackTrace();
            }
           
            System.out.println("B ");
      }

}
මේකේ තියන error වර්ගය දන්නේ නැති නිසා තමයි Exception කියල දෙන්නේ.එතොකොට එන output එකේ මේ වගේ පෙන්නනවා.

A
java.lang.StringIndexOutOfBoundsException: String index out of range: 6
        at java.lang.String.charAt(String.java:658)
        at Error.main(Error.java:14)
B

නමුත් අපිට පලුවන් catch කිහිපයක් යොදල එන error එක අල්ලගන්න.

public class Error {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            int a=5,b=0;
            int[] ar=new int[5];
            String name="Kasun";
           
            System.out.println("A");
            try{
                  ar[a]=(int)name.charAt(6)/b;
            }catch(StringIndexOutOfBoundsException e){
                  System.out.println("String Index Error");
                  e.printStackTrace();
            }catch(ArrayIndexOutOfBoundsException e){
                  System.out.println("Array Index Error");
                  e.printStackTrace();
            }catch(ArithmeticException e){
                  System.out.println("Arithmetic Error");
                  e.printStackTrace();
            }
            System.out.println("B ");
      }

}

Output එක මෙන්න මේ වගේ error එකට අදාල තැනින් එලියට පනිනවා.

A
String Index Error
B
java.lang.StringIndexOutOfBoundsException: String index out of range: 6
      at java.lang.String.charAt(Unknown Source)
      at Error.main(Error.java:14)

Nested try –catch
try –catch එකක් ඇතුලේ තව try catch එක use කරනවට තමයි Nested try –catch කියල කියන්නේ.කිසිම වෙනසක් නැතුව කලින් ඒවා වගේ තමයි වැඩ කරන්නේ.මේ ප්‍රෝග්‍රෑම් එකේ කරලා තියන්නේ කලින් වෙනවෙනම කරපු ප්‍රෝග්‍රෑම් දෙක එකතුකරලා.

public class Error {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            int a=5,b=0;
            int[] ar=new int[5];
            String name="kasun";
           
            System.out.println("A");
            try{
                  int x=42/b;
                  System.out.println("Outer try");
                  try{
                        ar[5]=name.charAt(6);
                        System.out.println("Inner try");
                  }catch(StringIndexOutOfBoundsException e){
                        System.out.println("String Index Error");
                        e.printStackTrace();
                  }
            }catch(ArithmeticException e){
                  System.out.println("Any Exception");
                  e.printStackTrace();
            }    
            System.out.println("B ");
      }

}

Finally block
finally block එක use කරන්නේ multi catch එකක් එක්ක.අපි try –catch එකක් use කරන්නේ රන් time error එකක් එන විටදී නේ.ඒ code කොටස රන් වෙලා හෝ නොවී අනිවාර්යෙන්ම රන් වෙන්න ඕන code එක දන්න තමයි finally block එකක් පාවිචි කරන්නේ.මේ ප්‍රෝග්‍රෑම් එකින් finally වලින් වෙන දේ පෙන්නනවා.


public class Error {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            int a=5,b=0;
            int[] ar=new int[5];
            String name="Kasun";
           
            System.out.println("A");
            try{
                  ar[a]=(int)name.charAt(6)/b;
            }catch(StringIndexOutOfBoundsException e){
                  System.out.println("String Index Error");
                  e.printStackTrace();
            }catch(ArrayIndexOutOfBoundsException e){   //
                  System.out.println("Array Index Error");
                  e.printStackTrace();
            }catch(ArithmeticException e){
                  System.out.println("Arithmetic Error");
                  e.printStackTrace();
            }catch(RuntimeException e){
                  System.out.println("Any runtime Error");
                  e.printStackTrace();
            }finally{
                  System.out.println("finally block");
            }
            System.out.println("B ");
      }

}

Throws clause

අපිට throws use කරලා ප්‍රෝග්‍රෑම් එකේ එන run time error එකක් throw කරන්න පලුවන්.මේ ප්‍රෝග්‍රෑම් එක බලන්න.

import java.io.IOException;

public class Error {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            Files f=new Files();
            try{
                  f.sentFile();
            }catch(IOException e){
                  //handle   
            }
      }

}

class Byte{
      void readBytes()throws IOException{
            System.out.println("read bytes...");
           
      }
}
class Files{
      void readData()throws IOException {
            Byte b=new Byte();
            b.readBytes();
            //
      }
      void createFile()throws IOException{
            readData();
            //
      }
      void sentFile() throws IOException{
            createFile();
            //
      }
}
මේ Byte class එකේ throw කරන error එක පිළිවලින් File class එකේ එක එක method වලට පනිනවා නමුත් අන්තිමට main method එකේදී error එක try –catch clause එකකින් අල්ලාගෙන handel කරනවා.අපිට ඕනෙනම් පලුවන් එතනුනිත් throw කරන්න. නමුත් සාමාන්‍ය ක්‍රමය තමයි throw කරගෙන ඇවිල්ල අන්තිමට try –catch එකින්න් error එක handel කරන එක.

මං හිතන්නේ ඔයාලට error handeling ගැන පොඩි දැනුමක් මේ පොස්ට් එකින් ලැබෙන්න ඇති කියල.නැවත අලුත් පාඩමකින් හමුවෙමු....


No comments: