දැනට තිබෙන පාඩම්...

Wednesday, September 16, 2015

ජාවා ඉගෙන ගනිමු - 4 පාඩම

යාලුවනේ අද අපි කථා කරන්නෙ Exception Handling කියන පාඩම ගැන


මුලින්ම අපි කථා කලොත් errors ගැන, errors ප්‍රධාන කොටස් 3යි.
1. Syntax errors
2. Run-time errors
3. Logic errors

Syntax errors එන්නෙ programme එකේ අපි type කරන code එක type කරන්න ඕන පිලිවෙල මාරු වුනාම,අපි කියන්නෙ spelling mistakes කියලා. Logic errors එන්නෙ හුගක්ම programme එකේ logic එක මාරු වුනාම, ඒ වගේම Run-time errors එන්නෙ programme එක compile වෙලා run වෙන්න පටන් ගන්න කොටයි.

java වලදි අපි කථා කරන මේ exception handling කියන කොටස නිර්මාණය වෙන්නෙ Object කියන class එකෙන්. මේ class එකේ child class එකක් වුන Throwable class එකට ප්‍රධානව Error class හා Exception class කියලා child class දෙකක් තියෙනවා. මේ Exception class එක SQLException , IOException හා Runtime කියලා ප්‍රධාන කොටස් 3යි. අපි කලින් කියපු Error class එකයි මේ Runtime class එකයි දෙකම Unchecked Exceptions වලටත් SQLException හා IOExceptions දෙක Checked Exceptions වලටත් අයිති වෙනවා.exceptions handle කරන්න අපි පාවිච්චි කරන්නෙ Checked exceptions කියන වර්ගයයි.
Runtime exceptions ප්‍රධාන කොටස්  2යි. NullpointerException සහ NumberFormatException කියන්නෙ ඒ දෙක.

මේ Exceptions handling කරන ප්‍රධාන ක්‍රම 2යි.
1. Try-Catch ක්‍රමය
2. Throwing ක්‍රමය

1. Try-Catch ක්‍රමය
මේකෙදි වෙන්නෙ අපිට ඕන කරන statement එක try කරනකොට ඒකෙ error එකක් ආවොත් ඒක ඊට පස්සෙ catch කරනවා.

try{
//අපිට error එයි කියල හිතෙන statement එක
}
catch{
           //එන්න ඕන error එක
          }

අපි ගත්තොත් programme එකක් උදාහරණෙට
   
class ExeptDemo
{
public static void main(String args[])
  {
     int i=5;
     int j=0;

     int k=i/j;
     System.out.println(k);
  }
}

අපි මේක run ක‍ලොත් මේකෙ error එකක් එනවා!
ඒක  compile වුනාට run වෙන්නෙ නෑ. මේකෙ එන්නෙ  Arithmetic exception කියන error එක.

අපි මේකට try catch දැම්මොත්...

class ExeptDemo
{
public static void main(String args[])
  {
     try{
         int i=5;
         int j=0;

         int k=i/j;
              System.out.println(k);
     }

   catch(Exception err)
            {
               err.getMessage();
            }
       }
}

මේකෙ exception දැම්මට පස්සෙ ඒ error එක එන්නෙ නෑ.

finally keyword එක අපිට මේකෙදි පාවිච්චි කරන්න පුලුවන්.මේ finally එක ඇතුලෙ type කරන ඕන දෙයක් try catch ඇතුලෙ මොනවා තිබුනත් print වෙනවා.

2. Throwing ක්‍රමය

මේ ක්‍රමයේ දි වෙන්නෙත් exception අයින් වෙන එක තමයි.අපි මේකට පාවිච්චි කරන්නෙ throw කියන keyword එක.

class ExeptDemo
{
public static void main(String args[]) throws exception
  {
     int i=5;
         int j=0;

         int k=i/j;
              System.out.println(k);

     }
    
       }
}

throw keyword එක අපි පාවිච්චි කරන්නෙ exception එකක් declare කරන්න.ඒත් throws keyword එ‍කෙන් exceptions කිහිපයක් වුනත් declare  කරන්න පුලුවන්.

throw new ArithmeticException("An integer should not be divided by zero!!")
throw new IOException("Connection failed!!")
throws IOException, ArithmeticException, NullPointerException,ArrayIndexOutOfBoundsException

තව මේකට එකතු වෙන්න දේවල් තියෙනවනම් කියන්න යාලුවනේ...

What is PowerShell -1

PowerShell mainly using by system administrators and system engineers to their daily work.  This task based command line mainly created by ...