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

सती रासो किसकी रचना है , sati raso ke rachnakar kaun hai in hindi , सती रासो के लेखक कौन है

सती रासो के लेखक कौन है सती रासो किसकी रचना है , sati raso ke…

13 hours ago

मारवाड़ रा परगना री विगत किसकी रचना है , marwar ra pargana ri vigat ke lekhak kaun the

marwar ra pargana ri vigat ke lekhak kaun the मारवाड़ रा परगना री विगत किसकी…

13 hours ago

राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए sources of rajasthan history in hindi

sources of rajasthan history in hindi राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए…

2 days ago

गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है ? gurjaratra pradesh in rajasthan in hindi

gurjaratra pradesh in rajasthan in hindi गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है…

2 days ago

Weston Standard Cell in hindi वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन

वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन Weston Standard Cell in…

3 months ago

polity notes pdf in hindi for upsc prelims and mains exam , SSC , RAS political science hindi medium handwritten

get all types and chapters polity notes pdf in hindi for upsc , SSC ,…

3 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