Need a project done?

C++ Programming Developer

Search This Blog

Exception out_of_range C++


1:  #include <iostream>  
2:  using namespace std;  
3:  #include <string>  
4:    
5:  int main(){  
6:       string sentence;  
7:       string str1, str2, str3;  
8:    
9:       try{  
10:            sentence = "Testing string exceptions!";  
11:            cout << "Sentence = " << sentence << endl;  
12:            cout << "sentence.lenght() = " << static_cast<int>(sentence.length()) << endl;  
13:            str1 = sentence.substr(8,20);  
14:            cout << "str1 = " << str1 << endl;  
15:            str2 = sentence.substr(28,10);  
16:            cout << "str2 = " << str2 << endl;  
17:            str3 = "Exception handling. " + sentence;  
18:            cout << "str3 = " << str3 << endl;  
19:            cout << "str3.length() = " << str3.length() << endl;  
20:       }  
21:    
22:       catch (out_of_range re){  
23:            cout << "In the out_of_range catch block: " << re.what() << endl;  
24:       }  
25:       catch(length_error le){  
26:            cout << "In the length error code block: " << le.what() << endl;  
27:       }  
28:    
29:       return 0;  
30:  }  

No comments:

Post a Comment

"Don't let anyone ever make you feel like you don't deserve what you want."