try, catch, এবং throw এর ব্যবহার

Exception Handling (এরর এবং এক্সসেপশন হ্যান্ডলিং) - সি++ স্ট্যান্ডার্ড লাইব্রেরি (C++ Standard Library) - Computer Programming

361

C++-এ try, catch, এবং throw ব্যবহার করে exception handling (ত্রুটি পরিচালনা) করা হয়। Exception handling একটি শক্তিশালী প্রোগ্রামিং কৌশল যা প্রোগ্রামে ত্রুটি বা ব্যতিক্রমের ঘটনা মোকাবেলা করতে ব্যবহৃত হয়, যেমন যদি কোনো নির্ধারিত শর্ত পূর্ণ না হয়, ফাইল না খোলা যায়, ডিভাইস থেকে ডেটা পড়া না যায়, বা কোনো অস্বাভাবিক পরিস্থিতি তৈরি হয়। throw ব্যবহার করে ত্রুটি সৃষ্টি করা হয়, এবং try-catch ব্লকগুলি সেই ত্রুটিকে ক্যাচ (ধরা) এবং পরিচালনা করতে ব্যবহৃত হয়।

1. throw: Exception উত্পন্ন করা

throw কিওয়ার্ডটি ব্যবহার করা হয় ত্রুটি বা ব্যতিক্রম সৃষ্টির জন্য। এটি একটি exception object তৈরি করে এবং এটি try ব্লকে পাঠানো হয়।

Syntax:

throw expression;
  • expression: এটি যে কোনও অবজেক্ট হতে পারে, যা exception হিসেবে কাজ করবে (যেমন একটি int, std::exception অবজেক্ট ইত্যাদি)।

উদাহরণ:

#include <iostream>

void checkNumber(int num) {
    if (num < 0) {
        throw "Negative number exception!"; // exception throw করা
    }
}

int main() {
    try {
        checkNumber(-1);  // এখানে ত্রুটি ঘটবে
    }
    catch (const char* msg) {
        std::cout << "Caught exception: " << msg << std::endl;  // exception ধরা হয়েছে
    }

    return 0;
}

আউটপুট:

Caught exception: Negative number exception!

এখানে, throw কিওয়ার্ডের মাধ্যমে "Negative number exception!" একটি exception হিসেবে তৈরি করা হয়েছে এবং catch ব্লক তা ধরেছে এবং প্রিন্ট করেছে।


2. try: Exception চেক করা

try ব্লক একটি অংশ যেখানে প্রোগ্রামটি এমন কিছু কোড চালায় যেটি ত্রুটি সৃষ্টি করতে পারে। যদি কোনো ত্রুটি ঘটে, তাহলে catch ব্লক সেই exception ধরবে।

Syntax:

try {
    // কোড যা exception সৃষ্টি করতে পারে
}
catch (exception_type1 e1) {
    // exception_type1 ধরা হলে এটি কার্যকর হবে
}
catch (exception_type2 e2) {
    // exception_type2 ধরা হলে এটি কার্যকর হবে
}

উদাহরণ:

#include <iostream>

void division(int a, int b) {
    if (b == 0) {
        throw "Division by zero error!";  // যদি b 0 হয়, তাহলে exception throw হবে
    }
    std::cout << "Result: " << a / b << std::endl;
}

int main() {
    try {
        division(10, 0);  // এখানে division() ত্রুটি ঘটাবে
    }
    catch (const char* msg) {
        std::cout << "Caught exception: " << msg << std::endl;  // exception catch করা হবে
    }

    return 0;
}

আউটপুট:

Caught exception: Division by zero error!

এখানে, try ব্লকটি কোড চালানোর চেষ্টা করছে, কিন্তু যখন b == 0, তখন throw কিওয়ার্ডের মাধ্যমে একটি exception তৈরি হচ্ছে। catch ব্লক তা ধরছে এবং ত্রুটির বার্তা প্রিন্ট করছে।


3. catch: Exception Handling

catch ব্লকটি try ব্লক থেকে কোনো exception সৃষ্টির পরে সেই exception ধরার জন্য ব্যবহৃত হয়। এক বা একাধিক catch ব্লক থাকতে পারে যা বিভিন্ন ধরনের exception ধরতে পারে।

Syntax:

try {
    // কিছু কোড
}
catch (type1 e1) {
    // type1 exception handle করা
}
catch (type2 e2) {
    // type2 exception handle করা
}

উদাহরণ:

#include <iostream>

void processNumber(int num) {
    if (num == 0) {
        throw std::invalid_argument("Invalid argument: number cannot be zero.");
    } else if (num < 0) {
        throw std::out_of_range("Out of range: number cannot be negative.");
    }
    std::cout << "Processing number: " << num << std::endl;
}

int main() {
    try {
        processNumber(-5);  // এটি "out_of_range" exception throw করবে
    }
    catch (const std::invalid_argument& e) {
        std::cout << "Caught exception: " << e.what() << std::endl;
    }
    catch (const std::out_of_range& e) {
        std::cout << "Caught exception: " << e.what() << std::endl;
    }
    catch (...) {
        std::cout << "Caught unknown exception." << std::endl;
    }

    return 0;
}

আউটপুট:

Caught exception: Out of range: number cannot be negative.

এখানে, catch ব্লক দুটি আলাদা exception টাইপ (std::invalid_argument এবং std::out_of_range) ধরতে সক্ষম এবং যে exception ঘটবে তা সঠিকভাবে ধরবে।


4. Exception Classes

C++ তে std::exception হলো সকল exception এর মূল ক্লাস, এবং অন্যান্য exception ক্লাসগুলো এর সাথে ডেরাইভড (উপজাত) থাকে। আপনি নিজের exception ক্লাসও তৈরি করতে পারেন যা std::exception বা তার ডেরাইভড ক্লাস থেকে উত্তরাধিকারী হতে পারে।

উদাহরণ: Custom Exception Class

#include <iostream>
#include <stdexcept>

class MyException : public std::exception {
public:
    const char* what() const noexcept override {
        return "This is a custom exception.";
    }
};

int main() {
    try {
        throw MyException();
    }
    catch (const MyException& e) {
        std::cout << "Caught custom exception: " << e.what() << std::endl;
    }

    return 0;
}

আউটপুট:

Caught custom exception: This is a custom exception.

এখানে, MyException একটি কাস্টম exception ক্লাস, যা std::exception থেকে উত্তরাধিকারসূত্রে এসেছে এবং what() মেথডটি পুনরায় সংজ্ঞায়িত করা হয়েছে।


5. Multiple Catch Blocks

একই try ব্লক থেকে একাধিক ধরনের exception ধরা যায়, এবং catch ব্লকগুলিকে নির্দিষ্ট exception টাইপ অনুযায়ী সাজানো যেতে পারে।

উদাহরণ:

#include <iostream>
#include <exception>

void throwExceptions(int choice) {
    if (choice == 1) {
        throw std::invalid_argument("Invalid argument exception.");
    } else if (choice == 2) {
        throw std::out_of_range("Out of range exception.");
    } else {
        throw std::exception();
    }
}

int main() {
    try {
        throwExceptions(2);  // এই ক্ষেত্রে out_of_range exception হবে
    }
    catch (const std::invalid_argument& e) {
        std::cout << "Caught exception: " << e.what() << std::endl;
    }
    catch (const std::out_of_range& e) {
        std::cout << "Caught exception: " << e.what() << std::endl;
    }
    catch (const std::exception& e) {
        std::cout << "Caught unknown exception: " << e.what() << std::endl;
    }

    return 0;
}

আউটপুট:

Caught exception: Out of range exception.

এখানে, যদি choice == 2 হয়, তবে std::out_of_range exception ধরা হবে।


উপসংহার

  • throw: একটি exception তৈরি করতে ব্যবহৃত হয় এবং এটি try ব্লকে পাঠানো হয়।
  • try: এই ব্লকটি কোডকে এমনভাবে চালায় যেখানে exception ঘটতে পারে।
  • catch: exception ধরা এবং এর সাথে কাজ করার জন্য ব্যবহৃত হয়। এটি এক বা একাধিক catch ব্লক দ্বারা handle করা যেতে পারে।

Exception handling C++ প্রোগ্রামিংয়ে গুরুত্বপূর্ণ, কারণ এটি প্রোগ্রামের ত্রুটি বা ব্যতিক্রমের পরিস্থিতি মোকাবেলা করতে সাহায্য করে, বিশেষত যখন প্রোগ্রামে অপ্রত্যাশিত ত্রুটি বা ভুল ঘটতে পারে।

Content added By
Promotion

Are you sure to start over?

Loading...