JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now

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

Class 6

Hindi social science science maths English

Class 7

Hindi social science science maths English

Class 8

Hindi social science science maths English

Class 9

Hindi social science science Maths English

Class 10

Hindi Social science science Maths English

Class 11

Hindi sociology physics physical education maths english economics geography History

chemistry business studies biology accountancy political science

Class 12

Hindi physics physical education maths english economics

chemistry business studies biology accountancy Political science History sociology

Home science Geography

English medium Notes

Class 6

Hindi social science science maths English

Class 7

Hindi social science science maths English

Class 8

Hindi social science science maths English

Class 9

Hindi social science science Maths English

Class 10

Hindi Social science science Maths English

Class 11

Hindi physics physical education maths entrepreneurship english economics

chemistry business studies biology accountancy

Class 12

Hindi physics physical education maths entrepreneurship english economics

chemistry business studies biology accountancy

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

four potential in hindi 4-potential electrodynamics चतुर्विम विभव किसे कहते हैं

चतुर्विम विभव (Four-Potential) हम जानते हैं कि एक निर्देश तंत्र में विद्युत क्षेत्र इसके सापेक्ष…

3 days ago

Relativistic Electrodynamics in hindi आपेक्षिकीय विद्युतगतिकी नोट्स क्या है परिभाषा

आपेक्षिकीय विद्युतगतिकी नोट्स क्या है परिभाषा Relativistic Electrodynamics in hindi ? अध्याय : आपेक्षिकीय विद्युतगतिकी…

4 days ago

pair production in hindi formula definition युग्म उत्पादन किसे कहते हैं परिभाषा सूत्र क्या है लिखिए

युग्म उत्पादन किसे कहते हैं परिभाषा सूत्र क्या है लिखिए pair production in hindi formula…

7 days ago

THRESHOLD REACTION ENERGY in hindi देहली अभिक्रिया ऊर्जा किसे कहते हैं सूत्र क्या है परिभाषा

देहली अभिक्रिया ऊर्जा किसे कहते हैं सूत्र क्या है परिभाषा THRESHOLD REACTION ENERGY in hindi…

7 days ago

elastic collision of two particles in hindi definition formula दो कणों की अप्रत्यास्थ टक्कर क्या है

दो कणों की अप्रत्यास्थ टक्कर क्या है elastic collision of two particles in hindi definition…

7 days 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