ජාවා with නානා (part 12)

Share

සෑහෙන කාලෙකින් ඔන්න මම ආයෙත් ආවා ජාවා with නානා(part 12) අරගෙන.

කට්ටියට දැන් ජාවා මතකද දන්නෙත් නෑ. අමතකනම් ඉතින් ආයෙත් මම ලියපු Tutorial ටික පොඩ්ඩක් බලන්න.

අන්තිමට මම sample program 2ක් කරා. අද මම සූදානම් වෙන්නේ ඊලග කොටසට යන්න. හැබැයි පොරොන්දු උන විදිහට 2 වෙනි example එක පැහැදිලි කරලා ඉන්නම්.

Sample Program 2
class Winchester2{
public static void main(String args[]){
int[]x={10,20,30,40,50,60,70,80};
System.out.println(x.length);
System.out.println(x[1]);
System.out.println(x[7]);
System.out.println(x[8]);
System.out.println(x[x[0]/2]);
}
}

ඔයාලට මතක ඇති, මේකේ output එක ආවේ මෙහෙම

java

මුල් උත්තර ටික නම් ඔයාලට අවුලක් නැතුව ඇති. අවුල තියනවනන් ඇත්තේ 4 වෙනි කේස් එකේ.

System.out.println(x[8]);

මේකයි මෙතන තියන ප්‍රශ්නේ. මම කිව්වනේ Array වල indexing පටන් ගන්නේ 0 ඉදන් කියල. එතකොට 8 කියලා index එකක් මේ array එකේ නෑ. එ වගේ වෙලාවට එන error එකක් තමයි ArrayIndexOutOfBoundException කියන්නේ. ඔය error එකේ නම හොදට මතක තියාගන්න. exam එකෙදි අනිවා අහයි.

ඊලගට ඔයාලට එන අනිත් ප්‍රශ්නෙ තමයි, ඇයි ඊලග statement එක execute වෙලා නැත්තේ කියලා. Exception එකකින් පස්සේ ඉතුරු statement execute වෙන්නේ නෑ.

එහෙනම් අපි දැන් යමු ඊලග කොටසට.

    Types of Errors

There are normally three types of errors in java

1) Syntax errors
2) Runtime errors
3) Logic errors

පහලට යන්න යන්න error එක භයානක වෙනවා

    Syntax errors

මේකට අදාල වෙන්නේ grammar mistake වගේ දේවල්. උදාහරණ කීපයක් තමයි

1.Misspelled variable and function names
2.Missing semicolons
3.Improperly matches parentheses, square brackets, and curly braces
4.Incorrect format in selection and loop statements

    Runtime errors

මේක වෙන්නේ machine එකට කරන්න බැරි දේවල් කරන්න කියලා command එක දුන්නම. උදාහරණ කීපයක් තමයි

1.Trying to divide by a variable that contains a value of zero
2.Trying to open a file that doesn’t exist
3.There is no way for the compiler to know about these kinds of errors when the program is compiled.

    Logic errors

මේකට උදාහරණ කීපයක් තමයි

1.Multiplying when you should be dividing
2.Adding when you should be subtracting
3.Opening and using data from the wrong file
4.Displaying the wrong message

Reference-http://www.dickbaldwin.com
Image – http://jacoudesgins.deviantart.com

අදට මම මෙතනින් නවත්වනවා.අලුත් ටියුටොරියල් එකෙන් අද හෙටම හමුවෙමු.
මොකක් හරි අවුලක් තියනවනම්
e-mail- charith_shaddow@yahoo.com
Facebook- charith shadow Winchester (naana)
දෙකෙන් එකකට message එකක් දාන්න.