Warning: Trying to access array offset on value of type null in /home/u623081920/domains/hindilearn.in/public_html/tutorials.php on line 241

Warning: Trying to access array offset on value of type null in /home/u623081920/domains/hindilearn.in/public_html/tutorials.php on line 244
Exception Handling In Hindi - C++ Programming - Hindilearn

आपकी ऑफलाइन सहायता

BACK
49

सी प्रोग्रामिंग

149

पाइथन प्रोग्रामिंग

49

सी प्लस प्लस

99

जावा प्रोग्रामिंग

149

जावास्क्रिप्ट

49

एंगुलर जे.एस.

69

पी.एच.पी.
माय एस.क्यू.एल.

99

एस.क्यू.एल.

Free

एच.टी.एम.एल.

99

सी.एस.एस.

149

आर प्रोग्रामिंग

39

जे.एस.पी.





डाउनलोड पी.डी.एफ. ई-बुक्स
C++ - Exception Handling

Runtime Errors


Exceptions ये Error का एक प्रकार है जो Run time पर या execution के time पर आ जाता है | जब program के किसी specific code को handle नहीं किया जा सकता या असामान्य स्थिति होने पर exception handling का इस्तेमाल किया जाता है |

इन Exceptions में divided by zero, out of bound array, out of memory या अन्य कारणों की वजह भी हो सकती है |


Exceptions दो प्रकार के होते है |

  • Compile-time Error
  • Run-time Exceptions

Compile-time Errors : compile time के वक्त error आ जाने पर Compile-time Errors कहते है | For Example, Logical Errors, Syntax Errors.

Run-time Errors(Exceptions) : Run-time के वक्त error आ जाने पर Exceptions कहते है | For Example, divided by zero, out of memory, array out of bound.

जब Program successfully execute होता है तब भी Exception या run time error आ सकता है | आप इस्तेमाल कर रहे IDE(Application) crash होने पर भी ये आ सकता है |

Simple Example for Exception

Example पर 'Divided by zero' exception occur हुआ है | इसे try-catch block द्वारा handle किया जायेगा |

#include <iostream>
using namespace std;

int main(){
int a = 5;
int b = 0;
int c;
 c = a/b;
 return 0;
}
Output :
[Warning] division by zero [-Wdiv-by-zero]

Try to Handle Exception/Run-time Error

Exceptions हो handle करने के लिए 'try catch throw' statement का इस्तेमाल किया जाता है |

  • try : जिस source code से exception आ जाने की संभावना होती है वो source code यहाँ पर दिया जाता है | इस try block से exception को throw किया जाता है |
  • throw : throw keyword का इस्तेमाल exception को throw करने के लिए किया जाता है | ये error के बारे में information को provide करता है | throw keyword के साथ दिए हुए parameter को handler पर pass किया जाता है |
  • catch : throw statement द्वारा throw किये हुए exception को catch करने के लिए catch block का इस्तेमाल किया जाता है | catch block पर exception को handle किया जाता है |

Syntax for try-catch Block

try{
 some_statements;

 throw exception_parameter;
}
catch (data_type e){
 some_statements;
}

Syntax for multiple catch Block

try{
 some_statements;

 throw exception_parameter;
}
catch (data_type e1){
 some_statements;
}
catch (data_type e2){
 some_statements;
}
---------------
catch (data_type eN){
 some_statements;
}

Handle Divided By Zero Exception(Single catch Block)

#include <iostream>
using namespace std;

int main(){
int a = 5;
int b = 0;
int c;
 try{
  if(b == 0){
   throw b;
  }
  c = a/b;
   cout<<"Value of c : " <<c;
 }
 catch(int ex){
  cout<<"You cannot declare "<<ex<<" as denominator.";
 }
 return 0;
}
Output :
You cannot declare 0 as denominator.

Example for Multiple catch Block

#include <iostream>
using namespace std;

int main(){
int a = 5;
int b = 10;
int c;
 try{
  if(b == 0){
   throw b;
  }else if(b > a){
    throw "Not allowed - denominator is greater than numerator.";
  }
  c = a/b;
   cout<<"Value of c : " <<c;
 }
 catch(int ex1){
  cout<<"You cannot declare "<<ex1<<" as denominator.";
 }
  catch(char const* ex2){
  cout<<ex2;
 }
 return 0;
}
Output :
Not allowed - denominator is greater than numerator.

ExceptionsDescription
bad_allocMemory allocate करने में असफल होने पर exception throw किया जाता है |
bad_castdynamic cast करने में असफल होने पर exception throw किया जाता है |
bad_exceptionunexpected handler द्वारा exception throw किया जाता है |
bad_function_callbad call पर exception throw किया जाता है |
bad_typeidtypeid द्वारा exception throw किया जाता है |
bad_weak_ptrBad weak pointer होने पर exception throw किया जाता है |
ios_base::failureये stream exception का base class होता है |
logic_errorsource code को read करने में असफल होने पर exception throw किया जाता है |
runtime_errorRuntime होने पर exception throw किया जाता है |
domain_errorinvalid domain होने पर exception throw किया जाता है |
future_errorfuture error होने पर exception throw किया जाता है |
invalid_argumentinvalid argument होने पर exception throw किया जाता है |
length_errorlength error होने पर exception throw किया जाता है |
out_of_rangeout of range होने पर exception throw किया जाता है |
overflow_errorArithmetic overflow error होने पर exception throw किया जाता है |
range_errorinternal computation में range error होने पर exception throw किया जाता है |
system_errorSystem error होने पर exception throw किया जाता है |
underflow_errormathematical underflow error होने पर exception throw किया जाता है |