Standard exceptions C++ In Hindi

Standard exceptions C++ In Hindi

C++ प्रोग्रामिंग लैंग्वेज में बिल्ट-इन स्टैंडर्ड क्लास लाइब्रेरी इसके यूजर को पहले से फिक्स्ड प्री-डिफाइन C++ प्रोग्राम में मल्टीप्ल एक्सेप्शन क्लास की एक हायरार्की या स्ट्रक्चर लाइब्रेरी को प्रोवाइड करता है. जो C++ प्रोग्रामिंग में सभी बेस या पैरेंट क्लास std::exception से आटोमेटिक क्लास में डिराइव्ड होती हैं। इन स्टैंडर्ड एक्सेप्शन क्लास लाइब्रेरी का यूज़ सभी C++ प्रोग्राम के एग्जीक्यूशन प्रोसेस के दौरान होने वाली मल्टीप्ल टाइप की प्रोग्रामिंग मिस्टेक्स और कुछ स्पेशल प्रोग्रामिंग कंडीशन को रिप्रेजेंट करने में किया जाता है।

Standard exceptions C++ In Hindi

So, let’s explore Standard Class Library exceptions in C++ programming in detail.

std::exception — base Class Concept in C++.

C++ प्रोग्रामिंग में std::exception क्लास एक बिल्ट-इन C++ प्रोग्राम में सभी स्टैंडर्ड क्लास एक्सेप्शन टाइप के लिए बेस या रुट क्लास की तरह वर्क करती है। यह C++ यूजर को एक what() मेंबर फंक्शन प्रोवाइड करता है. जो किसी क्लास में डिस्प्ले होने वाली एक्सेप्शन को इंडीकेट या रिप्रेजेंट करने वाले C-स्टाइल स्ट्रिंग वैल्यू को रिटर्न करता है।

std::exception — syntax of the Base Class.

class exception {

public:

virtual const char* what() const noexcept;

};

Explanation std::exception of the Base Class.

  • यहाँ इस सिंटेक्स में what() फंक्शन किसी क्लास का एक एक्सेप्शन के डिस्क्रिप्शन को वैल्यू इनफार्मेशन को रिटर्न करता है। यह मौजूदा सिंटेक्स में एक वर्चुअल फंक्शन की तरह डिफाइन है, और इसे मौजूदा क्लास में एक स्पेशल यूनिक क्लास एरर मैसेज को डिस्प्ले करने के लिए डिराइव्ड क्लास में इसे ओवरराइड किया जा सकता है।
  • वही noexcept एक क्लास स्पेसिफायर में यह इंडीकेट करता है कि यह फंक्शन किसी क्लास में कोई एक्सेप्शन को थ्रो नहीं करता है।

Derived Exception Class Concept in C++.

C++ प्रोग्राम में स्टैंडर्ड एक्सेप्शन्स लाइब्रेरी मौजूदा क्लास में कई एक्सेप्शन क्लास को डिफाइन करती है, जो मौजूदा प्रोग्राम में std::exception से आटोमेटिक इनहेरिट होती हैं। ये एक्सेप्शन मेथड मल्टीप्ल टाइप की मिस्टेक्स को क्लास प्रोग्राम में डिस्प्ले करते हैं, जो किसी C++ प्रोग्राम में एक कॉमन मेथड हैं।

Here are some of the most commonly used standard exception class methods in a C++ program.

std::runtime_error exception in C++.

यह C++ प्रोग्राम एक्सेप्शन प्रोग्राम रन एक्सेक्यूशन प्रोसेस के दौरान होने वाली कॉमन प्रोग्राम मिस्टेक्स और इश्यूज को डिस्प्ले करते है. जैसे कि, मौजूदा C++ प्रोग्राम में फाल्स वैल्यू इनपुट होना, लॉजिकल एक्सप्रेशन कंडीशन मिस्टेक्स, या प्रोग्राम रनटाइम में होने वाले कॉमन इश्यूज, आदि है।

std::runtime_error एक्सेप्शन को C++ प्रोग्राम में सामान्य रूप से तब यूज़ किया जाता है. जब C++ प्रोग्राम में कोई ऐसी मिस्टेक्स डिस्प्ले होती है. इस वजह से वह प्रोग्राम में रनटाइम पर करेक्ट नहीं हो पाती है।

std::runtime_error Constructor method.

std::runtime_error(const std::string& what_arg);

Example of a std::runtime_error exception.

#include <iostream>

#include <stdexcept>  // this preprocessor header file use for std::runtime_error exception

using namespace std;

void testDivision(int divisor_value) {

    if (divisor_value == 0) {

        throw runtime_error(“Divide any integer value with zero is not permitted”);

    }

}

int main() {

    try {

        testDivision(0);  // in try catch block it Throws a runtime_error exception error

    } catch (const runtime_error& e) {

        cout << “Division Value Caught exception is – ” << e.what() << endl;

    }

    return 0;

}

Explanation of a std::runtime_error exception.

  • Division Value Caught exception is – Divide any integer value with zero is not permitted

std::logic_error exception in C++.

किसी C++ प्रोग्राम में std::logic_error एक्सेप्शन मौजूदा क्लास प्रोग्राम में यूज़ अप्लाई प्रोग्राम लॉजिक एक्सप्रेशन कंडीशन में जनरेट होने वाली मल्टीप्ल प्रोग्राम मिस्टेक्स को रिप्रेजेंट करता है। ये लॉजिकल एक्सप्रेशन मिस्टेक्स सामान्य रूप से C++ प्रोग्राम में डिज़ाइन डेवलपमेंट में प्रॉब्लम या फाल्स अनुमानों को डिस्प्ले करता हैं।

यहाँ std::logic_error एक्सेप्शन्स एक्साम्प्ल में आउट-ऑफ़-बाउंड्स ऐरे डाटा एक्सेस, प्रीकंडीशन का वायलेशन होना, या इनवेरिएंट का वायलेशन, आदि इन्क्लुड हैं।

std::logic_error exception constructor.

std::logic_error(const std::string& what_arg);

Example of std::logic_error exception.

#include <iostream>

#include <stdexcept>  // this preprocessor header file use for std::logic_error exception

using namespace std;

void analyzeLogicExcpton(bool logic) {

    if (!logic) {

        throw logic_error(“display Logic error, user define logic Condition is false”);

    }

}

int main() {

    try {

        analyzeLogicExcpton(false);  // in try catch block it Throws a logic_error exception error

    } catch (const logic_error& e) {

        cout << “it Caught logical exception – ” << e.what() << endl;

    }

    return 0;

}

Explanation of std::logic_error exception.

  • it Caught logical exception – display Logic error, user define logic Condition is false

std::out_of_range exception in C++.

यह C++ प्रोग्राम एक्सेप्शन किसी प्रोग्राम में तब थ्रो होती है, जब मौजूदा प्रोग्राम में किसी ऐसे वेक्टर डाटा एलिमेंट वैल्यू को एक्सेस या ट्राय करने की कोशिश की जाती है. जो मौजूदा प्रोग्राम में डिफाइन किसी ऐरे, वेक्टर, या अन्य डाटा टाइप कंटेनर की वैलिड रेंज से बाहर डिफाइन होता है।

यह एक्सेप्शन मौजूदा प्रोग्राम में एक तरह का std::logic_error आउट ऑफ़ रेंज एरर मैसेज है. जो किसी प्रोग्राम एक्सेक्यूशन के दौरान इनवैलिड डाटा रेंज वैल्यू वैल्यू एक्सेस का सिग्नल डिस्प्ले करता है।

std::out_of_range exception constructor.

std::out_of_range(const std::string& what_arg);

Example of a std::out_of_range exception.

#include <iostream>

#include <stdexcept>  // this preprocessor header file use for std::out_of_range exception

#include <vector>

using namespace std;

int main() {

    vector<int> vctr = {7, 4, 5, 6};

    try {

        // here it Accessing an element out of bounds vector messages

        cout << vctr.at(9) << endl;  // in try catch block it Throws std::out_of_range vector bound exception error

    } catch (const out_of_range& e) {

        cout << “it Caught exception – ” << e.what() << endl;

    }

    return 0;

}

Explanation of std::out_of_range exception

  • it Caught exception – vector::_M_range_check: __n (which is 9) >= this->size() (which is 4)

std::invalid_argument exception in C++.

यह C++ प्रोग्राम एक्सेप्शन किसी प्रोग्राम में तब डिस्प्ले होती है, जब मौजूदा C++ प्रोग्राम में किसी फ़ंक्शन को कोई इनवैलिड आर्गुमेंट वैल्यू एक्सप्रेशन मिलता है. जैसे कि, एक नेगेटिव नंबर वैल्यू को पास करना जहाँ सिर्फ़ आपको पॉज़िटिव नंबर वैल्यू पास की पॉसिबिलिटी हो। यह C++ प्रोग्राम में std::logic_error का ही एक टाइप एक्सेप्शन मैसेज है।

std::invalid_argument exception constructor.

std::invalid_argument(const std::string& what_arg);

Example of std::invalid_argument exception.

#include <iostream>

#include <stdexcept>  // this preprocessor header file use For std::invalid_argument exception

using namespace std;

void validatePositive(int numeric_value) {

    if (numeric_value <= 0) {

        throw invalid_argument(“Entered value Argument should be in positive number”);

    }

}

int main() {

    try {

        validatePositive(-9);  // in try catch block it Throws invalid_argument exception error

    } catch (const invalid_argument& e) {

        cout << “Caught exception – ” << e.what() << endl;

    }

    return 0;

}

Explanation of std::invalid_argument exception.

  • Caught exception – Entered value Argument should be in positive number

Leave a Reply