Need a project done?

C++ Programming Developer

Search This Blog

Showing posts with label exception. Show all posts
Showing posts with label exception. Show all posts

Exception bad_alloc C++


1:  #include <iostream>  
2:  using namespace std;  
3:  #include <string>  
4:    
5:  int main(){  
6:       int *list[100];  
7:    
8:       try{  
9:            for (int i = 0; i<100; i++){  
10:                 list[i] = new int[50000000];  
11:                 cout << "Created List[" << i << "] of 50000000 components." << endl;  
12:            }  
13:       }  
14:       catch(bad_alloc be){  
15:            cout << "In the bad_alloc catch bloc: " << be.what() << endl;  
16:       }  
17:       return 0;  
18:  }  

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:  }  
"Don't let anyone ever make you feel like you don't deserve what you want."