JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now

हिंदी माध्यम नोट्स

Categories: c++ language in hindi

Exception Advance in c++ language , learn Exception Advance level examples program

learn Exception Advance level examples program , Exception Advance in c++ language :-
इससे पहले के article मे , Exceptions handeling को discuss किया था अब इस article मे Exceptions handeling के लिए use किया गये जाने keyword को discuss करेगे |
Throwing Exceptions
Exceptions को किसी प्रोग्राम मे try block के अन्दर कही पर भी throw किया जा सकता है | इसके लिए throw keyword को use किया जाता है | किसी throw statement के oprand उस Exceptions मे pass किये गये expression के type और उस expression के आउटपुट के type को define करता है |इसके अलावा throw keyword के द्वारा फेके गये code के type को भी define किया जाता है |
उदाहरन के लिए , किसी Exceptions जिसमे किसी non zero value को zero से divind किया जाता है तब throw statement को निन्म तरह से use किया जाता है |
double division(int a, int b) {
if( b == 0 ) {
throw “zero condition Division condition “;
}
return (a/b);
}
जब ये code run होगा तब अगर किसी non zero value को zero से divind किया जाता है तब zero condition Division condition print जरुरु होता है |
Catching Exceptions
catch block हमेशा try block मे होता है | try block मे किसी exception को catch किया जाता है | इसमें exception के type को define किया जाता ही अतः जिस प्रकार के exception को declare किया गया है इसे {} block मे लिखा जाता है | इसके लिए निन्म syntax होता है :-
try {
// protected code
} catch( ExceptionName e ) {
// code to handle ExceptionName exception
}
upper वाले code मे , किसी exception के block मे exceptionname type जरुर होता है | catch block किसि प्रकार के exception को handel करता है जिसे try block मे फेके गया है | किसी exception declartion मे parentheses के बीच …… को जरुर लिखा जाता है | इसका code निन्म है |
try {
// protected code
} catch(…) {
// code to handle any exception
}
नीचे exception का उदहारण है जिसमे 1 को 0 से डेविड किया जाने वाले exception को handel किया गया है |
#include <iostream>
using namespace std;
double div(int a, int b) {
if( b == 0 ) {
throw “Division by zero condition!”;
}
return (a/b);
}
int main () {
int x = 50;
int y = 0;
double z = 0;
try {
z = div(x, y);
cout << z << endl;
} catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
इस उदाहरन मे exception का type const char* है इसलिए जब किसी exception को catch किया जाता है तब const char* को catch statement को लिखा जाता है |जब इस code को run किया जाता है तब निन्म आउटपुट आता है :
Division by zero condition!
Exception Handling in C++
नीचे दिए गये उदाहरण के लिए , Exception Handling के rule को discuss किया गया है :-
1) नीचे दिए गये उदाहरन मे , Exception Handling के फ्लो को समजा जा सकता है |
#include <iostream>
using namespace std;
int main()
{
int x = -5;
// Some code
cout << “Before try \n”;
try {
cout << “Inside try \n”;
if (x < 0)
{
throw x;
cout << “Unsigned Integer Never assign negetative value \n”;
}
}
catch (int x ) {
cout << “Exception Caught \n”;
}
cout << “Execution start \n”;
return 0;
}
इस code मे एक integer variable मे negatative value को assign किया जाता है जो की Exception का उदहारण है | इससे निन्म Exception Handling से handel किया जाता है |
Output:
Before try
Inside try
Unsigned Integer Never assign negetative value
Execution start
2)c++ मे , एक sepecial catch block होता है जिसे catch all block कहता है | इससे catch(…..) से define किया जाता है | इस catchtype से किसी प्रोग्राम मे उपस्थित  सभी Exception को catch किया जाता है | इसका उदाहरन निन्म है :
#include <iostream>
using namespace std;
void main()
{
try  {
throw 10;
}
catch (char *excp)  {
cout << “Exception found ” << excp;
}
catch (…)  {
cout << “Default Exception\n”;
}
getch();
}
इस code मे declare किये गये Exception को default Exception कहते है |
Output:
Default Exception
3) Implicit type conversion doesn’t happen for primitive types. For example, in the following program ‘a’ is not implicitly converted to int
filter_none
edit
play_arrow
brightness_4
#include <iostream>
using namespace std;
int main()
{
try  {
throw ‘a’;
}
catch (int x)  {
cout << “Exception Catch ” << x;
}
catch (…)  {
cout << “Default Exception\n”;
}
return 0;
}
Output:
Default Exception
4)जब किसी  Exception को throw किया जाता है और इसे कभी कभी भी catch नहीं किया जाता है तब प्रोग्राम  abnormal terminate हो जाता है | नीचे दिए गये उदहारण मे char को throw किया गया है और इससे कभी कभी catch नहीं किया गया है
#include <iostream>
using namespace std;
int main()
{
try  {
throw ‘a’;
}
catch (int x)  {
cout << “Exception Catch “;
}
return 0;
}
Output:
इस उदाहरबा मे terminate हो जाता है क्योकि char को throw किया गया है इससे कभी कभी भी catch नहीं किया गया है |
इस article मे , Exception को advance को discuss किया जाता है अब आगे के article मे , Exception के advance को discuss करेगे |
Sbistudy

Recent Posts

मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi

malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…

1 month ago

कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए

राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…

1 month ago

हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained

hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…

2 months ago

तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second

Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…

2 months ago

चौहानों की उत्पत्ति कैसे हुई थी ? chahamana dynasty ki utpatti kahan se hui in hindi

chahamana dynasty ki utpatti kahan se hui in hindi चौहानों की उत्पत्ति कैसे हुई थी…

2 months ago

भारत पर पहला तुर्क आक्रमण किसने किया कब हुआ first turk invaders who attacked india in hindi

first turk invaders who attacked india in hindi भारत पर पहला तुर्क आक्रमण किसने किया…

2 months ago
All Rights ReservedView Non-AMP Version
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings
JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now