Custom exceptions c++ In Hindi
C++ प्रोग्रामिंग लैंग्वेज में किसी यूजर क्रिएटेड C++ प्रोग्राम को क्रैश या डैमेज किए बिना प्रोग्राम एक्सेक्यूशन टाइम पर रनटाइम कस्टम यूजर डिफाइन एरर एक्सेप्शन मैसेज को हैंडल या मैनेज करने के लिए C++ प्रोग्राम में कस्टम एक्सेप्शन को अप्लाई किया जाता है। जैसा कि पुर्व में हमने C++ में कई स्टैंडर्ड एक्सेप्शन क्लास (जैसे std::runtime_error और std::invalid_argument) को समझा और जाना है. C++ प्रोग्रामर सॉफ्टवेयर डेवलपर्स किसी पर्टिकुलर एप्लिकेशन-स्पेसिफिक प्रोग्राम एरर को डिस्प्ले करने के लिए एक कस्टम एक्सेप्शन भी क्रिएट कर सकते हैं।

C++ प्रोग्रामिंग में यूजर क्रिएटेड कस्टम एक्सेप्शन एक यूज़र-डिफाइंड क्लास मेथड है. जो किसी प्रोग्राम में तब थ्रो होती है, जब C++ यूजर को कोई स्पेशल एरर डिस्प्ले करना होता है, जिसमे try-catch ब्लॉक मेथड का यूज़ करके इन कस्टम प्रोग्राम एक्सेप्शन को कैच किया जाता है।
Why use custom exceptions in C++ programming?
- C++ प्रोग्रामिंग में कस्टम एक्सेप्शन C++ यूजर को कुछ पर्टिकुलर एक्सेप्शन इश्यूज एरर को डिस्प्ले करने में आपको हेल्प करते हैं.
- मौजूदा प्रोग्राम में कुछ पर्टिकुलर स्पेसिफिक कस्टम प्रोग्राम एरर कंडीशन को डिस्प्ले करने में।
- किसी C++ प्रोग्राम सोर्स कोड लॉजिक एक्सप्रेशन को अंडरस्टैंड और मेंटेनेंस इजी करने आदि में।
- कुछ स्पेसिफिक एसेंशियल इम्पोर्टेन्ट कस्टम प्रोग्राम एक्सेप्शन एरर मैसेज को डिस्प्ले करने में।
- सामान्य C++ प्रोग्राम में यूज़ लॉजिक एक्सप्रेशन को कस्टम एरर हैंडलिंग से अलग कर डिस्प्ले करने में।
Examples of custom exceptions in a C++ program.
- इनवैलिड कस्टमर ID डिटेल चेक करने में
- कस्टमर बैंक अकाउंट अमाउंट बैलेंस चेक करने में
- किसी पेशेंट की ऐज और डिजीज चेक करने में
- न्यूमेरिक कस्टम कैलकुलेशन में ज़ीरो से वैल्यू को डिवीज़न चेक करने में
Creating a custom exception in a C++ program.
C++ प्रोग्रामिंग लैंग्वेज में एक कस्टम प्रोग्राम एक्सेप्शन सिर्फ एक यूजर डिफाइन कस्टम क्लास होती है। जो सामान्य रूप से एक स्टैंडर्ड एक्सेप्शन क्लास से आटोमेटिक इनहेरिट होता है.
#include <exception>
class TestException : public std::exception
{
public:
const char* what() const noexcept override
{
return “Custom Exception testing”;
}
};
Explanation of a custom exception in a C++ program.
- यहाँ इस एक्साम्प्ल में public std::exception एक स्टैंडर्ड एक्सेप्शन क्लास बिहेवियर और प्रॉपर्टीज को मौजूदा प्रोगाम में इनहेरिट करता है।
- what() फंक्शन मौजूदा प्रोग्राम में एक एरर मैसेज इनफार्मेशन को डिस्प्ले या रिटर्न करता है।
- noexcept स्टेटमेंट मौजूदा प्रोगाम में यह इंडिकेट करता है कि इस प्रोग्राम में what() फंक्शन कोई अन्य एक्सेप्शन को थ्रो नहीं करेगा।
- override स्टेटमेंट इस प्रोगाम में यह कन्फर्म करता है कि यह फंक्शन मौजूदा बेस क्लास वर्जन को ओवरराइड करता है।
Throwing a custom exception in a C++ program.
C++ प्रोग्राम में throw रिजर्व्ड कीवर्ड का यूज़ कर आप एक कस्टम प्रोग्राम एक्सेप्शन एरर मैसेज को थ्रो कर सकते है।
throw TestException();
Example of throwing a custom program exception.
if (emp_age < 21)
{
throw TestException();
}
Let Catching a Custom Exception.
try
{
throw TestException();
}
catch(const TestException& e)
{
cout << e.what();
}
Output of throwing a custom program exception.
Custom exception occurred
Complete example of throwing a custom program exception.
#include <iostream>
#include <exception>
using namespace std;
class EmpAgeException : public exception
{
public:
const char* what() const noexcept override
{
return “Age must be at least 40 for staff promotion.”;
}
};
int main()
{
int emp_age;
cout << “Enter employe age – “;
cin >> emp_age;
try
{
if(emp_age < 40 )
throw EmpAgeException();
cout << “Eligible for staff promotion.”;
}
catch(const EmpAgeException& e)
{
cout << e.what();
}
return 0;
}
throwing a custom program exception explanation.
Enter employe age – 2
Age must be at least 40 for staff promotion.
Enter employe age – 47
Eligible for staff promotion.
Custom exception example for customer bank account balance.
#include <iostream>
#include <exception>
using namespace std;
class InsufficientAmount : public exception
{
public:
const char* what() const noexcept override
{
return “Insufficient customer bank account balance.”;
}
};
int main()
{
double deposit_balance = 14000;
double withdraw_amount;
cout << “Please Enter your withdrawal amount – “;
cin >> withdraw_amount;
try
{
if(withdraw_amount > deposit_balance)
throw InsufficientAmount();
deposit_balance -= withdraw_amount;
cout << “Customer account remaining balance is = ” << deposit_balance;
}
catch(const InsufficientAmount& e)
{
cout << e.what();
}
return 0;
}
Explanation of Custom exception example for customer bank account balance.
Please Enter your withdrawal amount – 42342
Insufficient customer bank account balance.
Please Enter your withdrawal amount – 1234
Customer account remaining balance is = 12766
custom exception with a custom message.
किसी C++ प्रोग्राम में फिक्स्ड आउटपुट मैसेज वैल्यू इनफार्मेशन को रिटर्न करने के बदले, C++ यूजर अपना एक पर्सनल कस्टम मैसेज को स्टोर और डिस्प्ले कर सकते हैं।
Example of custom exception with a custom message.
#include <iostream>
#include <exception>
#include <string>
using namespace std;
class TestException : public exception
{
string mesg_info;
public:
TestException(string msg)
{
mesg_info = msg;
}
const char* what() const noexcept override
{
return mesg_info.c_str();
}
};
int main()
{
try
{
throw TestException(“Invalid Patient Record ID”);
}
catch(const TestException& e)
{
cout << e.what();
}
return 0;
}
Explanation of custom exception with a custom message.
Invalid Patient Record ID
Creating a custom exception in a C++ program without inheriting.
जैसा कि C++ प्रोग्राम में किसी भी टाइप की कस्टम एक्सेप्शन एरर मैसेज को थ्रो करना पॉसिबल है. लेकिन इसके लिए इसे बेस्ट std::exception क्लास मेथड से इनहेरिट करना रिकमेंडेड किया जाता है।
Example of Creating a custom exception in a C++ program without inheriting.
#include <iostream>
using namespace std;
class NonIntegerNumber
{
};
int main()
{
try
{
throw NonIntegerNumber();
}
catch(NonIntegerNumber)
{
cout << “Non Integer number value is not allowed.”;
}
return 0;
}
Explanation of creating a custom exception in C++ without inheriting.
Non Integer number value is not allowed.
Unique advantages of custom exceptions in C++ programs.
- कुछ काम्प्लेक्स C++ प्रोग्राम को डीबग और मेंटेनेंस करना इजी हो जाता है।
- C++ प्रोग्राम में यूजर मीनिंगफुल और स्पेसिफिक एरर मैसेज को डिस्प्ले कर सकते है।
- कस्टम एक्सेप्शन C++ प्रोग्राम सोर्स कोड रीडेबिलिटी को इम्प्रूव करता है।
- कस्टम एक्सेप्शन C++ प्रोग्राम में मल्टीप्ल इंडिविजुअल टाइप एरर को अलग-अलग हैंडल और मैनेज करने में हेल्प करता है।
- C++ प्रोग्राम में कस्टम एक्सेप्शन मॉड्यूलर प्रोग्रामिंग और प्रोग्राम मेंटेनेबल कोड को इनक्रीस करता है।
Detail Summary of statement or method in custom exception.
| Custom exception type | Custom program exception description |
| Custom exception | Custom exception used as user-defined class used to represent specific runtime custom program exception error messages. |
| Throw keyword | Throw keyword used to signal an custom program exception. |
| Try keyword | Try keyword contains user define program source code, that may throw an custom user error exception. |
| Catch keyword | Catch keyword use to handles the thrown keyword exception message or steps. |
| Std::exception class | This class library used as a base class for standard program custom exceptions; generally, it inherited by user generated custom exceptions. |
| What() function | What function used to returns a description of the custom exception as a c-style string (const char*) method. |
Custom exceptions summary in C++.
C++ प्रोग्रामिंग लैंग्वेज में कस्टम एक्सेप्शन C++ यूजर को अपने डिजायर प्रोग्राम सॉफ्टवेयर एप्लिकेशन के लिए कुछ कस्टम एक्सेप्शन एरर टाइप मैनेज करने में हेल्प करते हैं। std::exception क्लास लाइब्रेरी से इनहेरिट कर और what() फंक्शन मेथड को ओवरराइड करके, C++ यूजर एक कस्टम एरर हैंडलिंग को ऑर्गनाइज़्ड या ग्रुप, रीड करने लायक और मेंटेन करने लायक प्रोग्राम सोर्स कोड मेन्टेन करते हुए क्लियर एरर मैसेज को डिस्प्ले कर सकते हैं।

