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 किया जायेगा |
Output :#include <iostream> using namespace std; int main(){ int a = 5; int b = 0; int c; c = a/b; return 0; }
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)
Output :#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; }
Example for Multiple catch Block
Output :#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; }
Exceptions | Description |
---|---|
bad_alloc | Memory allocate करने में असफल होने पर exception throw किया जाता है | |
bad_cast | dynamic cast करने में असफल होने पर exception throw किया जाता है | |
bad_exception | unexpected handler द्वारा exception throw किया जाता है | |
bad_function_call | bad call पर exception throw किया जाता है | |
bad_typeid | typeid द्वारा exception throw किया जाता है | |
bad_weak_ptr | Bad weak pointer होने पर exception throw किया जाता है | |
ios_base::failure | ये stream exception का base class होता है | |
logic_error | source code को read करने में असफल होने पर exception throw किया जाता है | |
runtime_error | Runtime होने पर exception throw किया जाता है | |
domain_error | invalid domain होने पर exception throw किया जाता है | |
future_error | future error होने पर exception throw किया जाता है | |
invalid_argument | invalid argument होने पर exception throw किया जाता है | |
length_error | length error होने पर exception throw किया जाता है | |
out_of_range | out of range होने पर exception throw किया जाता है | |
overflow_error | Arithmetic overflow error होने पर exception throw किया जाता है | |
range_error | internal computation में range error होने पर exception throw किया जाता है | |
system_error | System error होने पर exception throw किया जाता है | |
underflow_error | mathematical underflow error होने पर exception throw किया जाता है | |