BREAKING NEWS
🚀 Kaweesha launched the new AI Tech Blog! 💻 Python & Java Tutorials are now live. 🎮 Game Development Guide: Building 2D Games with Godot. 🌐 Web Hosting secrets revealed - Check the latest post!

Java Sinhala - Part 12: සොෆ්ට්වෙයාර් Crash නොවී බේරගන්න මැජික් එක! (Exception Handling & Errors) 🛡️💥

Java Exception Handling and Error Management in Sinhala - Java Sinhala Part 12

අපි ජාවා පාඩම් මාලාවේ අංක 10 සහ 11 පාඩම් වලින් Object-Oriented Programming (OOP) වල ප්‍රධාන කණු 4 වෙන Encapsulation, Inheritance, Polymorphism සහ Abstraction ගැන පතුලටම කතා කළා. දැන් ඔයාට Java වලින් Class එකක් හදන්න, Object හදන්න, Polymorphism පාවිච්චි කරලා කෝඩ් එක Flexibly ලියන්න පුළුවන්.

හැබැයි මෙන්න මෙහෙම දෙයක් හිතන්න: ඔයා පට්ටම OOP Concept පාවිච්චි කරලා Banking System එකක් ලිව්වා කියමු. පරිශීලකයා (User) එකපාරටම එකවුන්ට් නොම්මරේ වෙනුවට "ABC" කියලා අකුරු වගයක් ටයිප් කළොත්? නැත්නම් ගණන් හදද්දී මොකක්හරි අගයක් 0 න් බෙදන්න වුණොත් (Divide by zero)? නැත්නම් Server එකත් එක්ක තිබ්බ Connection එක එකපාරටම ඩ්‍රොප් වුණොත්? 💥

අන්න ඒ වෙලාවට ඔයාගේ සොෆ්ට්වෙයාර් එක Red Color එකෙන් Error එකක් දීලා කෙලින්ම Off වෙනවා (Crash වෙනවා). Software Industry එකේදී මෙහෙම සොෆ්ට්වෙයාර් Crash වෙන එක ලොකුම අසාර්ථකවීමක්. මෙන්න මේ ප්‍රශ්නෙන් අපේ සොෆ්ට්වෙයාර් එක බේරගන්න තියෙන ආරක්ෂක පලිහට තමයි අපි Exception Handling කියලා කියන්නේ. අද KP Tech Gear Java Part 12 එකෙන් අපි මේ ගැන පතුලටම බහිමු!

💥 1. Error එකක් සහ Exception එකක් අතර වෙනස (Error vs Exception)

ජාවා වලදී කෝඩ් එක අවුල් යන අවස්ථා දෙකකට බෙදන්න පුළුවන්. ගොඩක් අය මේ දෙක පටලගන්නවා.

⚠️ Error (පාලනය කළ නොහැකි දරුණු දෝෂ):
මේවා වෙන්නේ System එකේ මට්ටමෙන්. උදාහරණයක් විදියට ඔයාගේ පරිගණකයේ RAM එක පිරී ඉතිරී ගිහින් මතකය මදි වුණොත් (OutOfMemoryError) නැත්නම් Loop එකක් පිස්සුවෙන් වගේ අනන්තයටම රන් වෙලා Memory එක overflow වුණොත් (StackOverflowError). මෙන්න මේවා Developer කෙනෙක්ට කෝඩ් එකෙන් වලක්වන්න බෑ. System එක Shutdown වීම හැර වෙන විකල්පයක් නෑ.
🛡️ Exception (කෝඩ් එකෙන් පාලනය කළ හැකි දෝෂ):
Exception එකක් කියන්නේ Program එකක් Run වෙන අතරතුර (Runtime) සිදුවන බලාපොරොත්තු නොවූ බාධාවකටයි. උදාහරණයක් විදියට File එකක් Read කරන්න යද්දී ඒ File එක නැති වීම (FileNotFoundException), නැත්නම් Array එකක නැති index එකකට කතා කිරීම (ArrayIndexOutOfBoundsException). මෙන්න මේවා අපිට කෝඩ් එක ඇතුලෙන්ම අල්ලාගෙන (Catch කරගෙන), සොෆ්ට්වෙයාර් එක Crash නොවී බේරාගන්න පුළුවන්!

🧬 2. Java Exception Hierarchy එක (Hierarchy Tree)

ජාවා වල තියෙන හැම Exception එකක්ම හැදිලා තියෙන්නේ Java Class Hierarchy එකකට අනුවයි. මේ හැමදේකම මුල් පියා (Super Class) වෙන්නේ java.lang.Throwable කියන Class එකයි.

  • Throwable
    • Error: (e.g., OutOfMemoryError, StackOverflowError)
    • Exception:
      • Checked Exceptions (Compile-time Exceptions): කෝඩ් එක Run කරන්නත් කලින්ම Java Compiler එක මඟින් අනිවාර්යයෙන්ම Handle කරන්න කියලා බලකරන Exceptions. (e.g., IOException, SQLException)
      • Unchecked Exceptions (Runtime Exceptions): කෝඩ් එක Run වෙන අතරතුර අපේ තර්කවල (Logic) තියෙන වැරදි නිසා එන Exceptions. RuntimeException එකෙන් Extend වෙනවා. (e.g., ArithmeticException, NullPointerException)

🛡️ 3. Exceptions පාලනය කරමු: The `try-catch-finally` Block

ජාවා වල Exception එකක් අල්ලගන්න අපි Keywords ප්‍රධාන 5ක් පාවිච්චි කරනවා: try, catch, finally, throw, සහ throws.

1. `try` Block එක

අපිට සැක සහිත, Error එකක් එන්න පුළුවන් කියලා හිතෙන කෝඩ් කොටස දාන්නේ මෙතනටයි.

2. `catch` Block එක

`try` එක ඇතුලේ අවුලක් ගියොත්, ඒ එන Exception එක අල්ලගෙන (Catch කරගෙන) කරන්න ඕනේ දේ මෙතන ලියනවා.

3. `finally` Block එක

Exception එකක් ආවත් නැතත්, අනිවාර්යයෙන්ම Run වෙනම කෝඩ් කොටස මෙතන ලියනවා. සාමාන්‍යයෙන් Database connection ක්ලෝස් කරන්න, Open කරපු Files ක්ලෝස් කරන්න (Resource Cleanup) මේක පාවිච්චි කරනවා.

public class ExceptionDemo {
    public static void main(String[] args) {
        System.out.println("Program එක පටන් ගත්තා...");

        try {
            int number1 = 10;
            int number2 = 0; // 0න් බෙදන්න බෑ!
            int result = number1 / number2; // මෙතනදී ArithmeticException එකක් එනවා
            System.out.println("පිළිතුර: " + result); // මේ ලයින් එක Run වෙන්නේ නෑ!
        } catch (ArithmeticException e) {
            // Exception එක අල්ලගත්තා! Program එක Crash වෙන්නේ නෑ.
            System.out.println("වැරදියි මචං! ගණන් හදද්දී 0න් බෙදන්න බෑ.");
            System.out.println("Error Message එක: " + e.getMessage());
        } finally {
            System.out.println("මේක මොන දේ වුණත් අනිවාර්යයෙන්ම Run වෙනවා!");
        }

        System.out.println("Program එක කිසිම අවුලක් නැතුව ඉවර වුණා.");
    }
}

🔥 4. Advanced: Multiple Catches සහ `try-with-resources`

කෝඩ් එකක් ඇතුලේ එකකට වඩා වෙනස් Exceptions එන්න පුළුවන් (උදාහරණයක් විදියට Array එකේ අවුලකුයි, බෙදීමේ අවුලකුයි දෙකම). අන්න ඒ වෙලාවට අපිට Multi-catch පාවිච්චි කරන්න පුළුවන්.

try {
    int[] arr = new int[5];
    arr[10] = 50 / 0; // මෙතන Exceptions දෙකක්ම එන්න පුළුවන්!
} catch (ArithmeticException e) {
    System.out.println("ගණිතමය දෝෂයක්!");
} catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("Array එකේ සීමාව පැනලා!");
} catch (Exception e) {
    // ඕනෑම පොදු Exception එකක් අල්ලගන්න (සාමාන්‍යයෙන් යටින්ම දාන්න ඕනේ)
    System.out.println("වෙනත් දෝෂයක් සිදු වුණා.");
}
💡 Pro-Tip: Java 7+ `try-with-resources`

අපි Scanner හරි File-Reader හරි පාවිච්චි කළාම scanner.close() කියලා finally block එක ඇතුලේ manualmente ක්ලෝස් කරන්න ඕනේ. හැබැයි Java 7 වලින් පස්සේ ආපු try-with-resources ක්‍රමයෙන් try(...) එක ඇතුලෙම Resource එක Open කළාම, වැඩේ ඉවර වුණු ගමන් ජාවා මඟින්ම ඒක Auto-close කරලා දෙනවා!

try (Scanner scanner = new Scanner(System.in)) { ... }

⚡ 5. `throw` සහ `throws` අතර තියෙන විශාල වෙනස

ගොඩක් Developers ලට පටලැවෙන තවත් තැනක් තමයි throw සහ throws කියන වචන දෙක.

ලක්ෂණය throw throws
අරමුණ කෝඩ් එක ඇතුලෙන් බලහත්කාරයෙන් Exception එකක් එලියට විසි කිරීමට පාවිච්චි කරයි. Method එකකින් Exception එකක් එන්න පුළුවන් බවට කලින්ම අවවාද කිරීමට (Declare කිරීමට) පාවිච්චි කරයි.
පාවිච්චි කරන ස්ථානය Method එකක් ඇතුලේ (Inside Method Body). Method Signature එකේ අගට (Method Header).
ප්‍රමාණය එක් වරකට එක් Exception Object එකක් පමණයි. (e.g., throw new Exception()) Commas වලින් වෙන් කර Exceptions කිහිපයක් දිය හැක. (e.g., throws IOException, SQLException)

🏦 6. අපේම Custom Exception එකක් හදමු! (Real-world Example)

ලොකු Software Projects හදද්දී, Java එකෙන් දෙන default exceptions මදි වෙනවා. උදාහරණයක් විදියට බැංකු සිස්ටම් එකක "ATM එකෙන් සල්ලි ගන්න යද්දී Balance එක මදි වීම" කියන්නේ Java එකට අදාල එකක් නෙවෙයි, ඒක අපේ Business Logic එකට අදාල එකක්. අන්න ඒ වගේ වෙලාවට අපිට Custom Exceptions හදාගන්න පුළුවන්!

// 1. Custom Exception Class එක සෑදීම (RuntimeException එකෙන් Extend කරමු)
class InsufficientBalanceException extends RuntimeException {
    public InsufficientBalanceException(String message) {
        super(message); // Super class එකට Error message එක යැවීම
    }
}

// 2. Bank Account Class එක
class BankAccount {
    private double balance = 5000.00; // ගිණුමේ ඇති මුදල

    public void withdraw(double amount) {
        if (amount > balance) {
            // Balance එක මදි නම් අපේ Custom Exception එක throw කරනවා
            throw new InsufficientBalanceException("සල්ලි මදි මචං! ඔබේ ශේෂය: " + balance);
        }
        balance -= amount;
        System.out.println(amount + " LKR ලබාගත්තා. ඉතිරි ශේෂය: " + balance);
    }
}

// 3. Main Class එක
public class BankApp {
    public static void main(String[] args) {
        BankAccount myAccount = new BankAccount();

        try {
            myAccount.withdraw(2000.00); // සාර්ථකයි
            myAccount.withdraw(4000.00); // මෙතනදී Exception එක එනවා!
        } catch (InsufficientBalanceException e) {
            System.err.println("ALERT: " + e.getMessage());
        }
    }
}

🧩 7. Bonus Section (කලින් පාඩම් වල මඟහැරුණු දේවල්): Wrapper Classes & Autoboxing

අපි Part 02 එකේදී Primitive Data Types (e.g., int, double, boolean) ගැන ඉගෙන ගත්තා නේද? හැබැයි OOP වලදී හැමදේම Objects වෙන්න ඕනේ. Primitive types කියන්නේ objects නෙවෙයි. මෙන්න මේ නිසා ජාවා වලින් හැම Primitive Data Type එකකටම අදාළව Wrapper Classes හඳුන්වා දුන්නා.

  • intInteger
  • doubleDouble
  • charCharacter
  • booleanBoolean

Autoboxing සහ Unboxing කියන්නේ මොකක්ද?

Autoboxing: Primitive අගයක් කෙලින්ම Wrapper Object එකක් බවට Automatic හැරවීම.
Integer myNum = 100; // int එකක් Integer object එකක් වුණා

Unboxing: Wrapper Object එකක තියෙන අගය නැවත Primitive බවට Automatic හැරවීම.
int num = myNum; // Integer object එක int බවට හැරුණා

මේවා ඉතාම වැදගත් වෙන්නේ ඉදිරි Part 13 එකේදී අපි කතා කරන්න යන **Java Collections (ArrayList, HashMap)** ඉගෙන ගනිද්දී, මොකද Collections ඇතුලේ සේව් කරන්න පුළුවන් Objects විතරක් නිසයි!

දැන් ඔයා Exception Handling අතින් සහ Java Object Handling අතින් Pro-Developer කෙනෙක්! 🛡️

ඊළඟ Java Sinhala Part 13 එකෙන් අපි කතා කරන්නේ, Data Structures වල රජ්ජුරුවෝ වෙන **Java Collections Framework (ArrayList, LinkedList, HashMap, HashSet)** ගැනයි. එකතැන දත්ත ගොඩක් ආරක්ෂිතව පාලනය කරන්නේ කොහොමද කියලා එදාට බලමු.

ලිපිය ගැන ඔයාගේ අදහස් සහ ප්‍රශ්න පල්ලෙහායින් කමෙන්ට් කරන්න. යාළුවන්ටත් ශෙයා කරන්න. ජය වේවා! 👇

Author

About

Tech enthusiast, Python/Java Developer, and Gamer. Sharing knowledge about coding and modern technology.

Discussion