Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a new statement in the try block which may throw the exception of length_e

ID: 3688086 • Letter: W

Question

Write a new statement in the try block which may throw the exception of length_error. Test the statement.

Submission guide:

Upload a program and a screenshot with your explanation about the output.

Here is what I have so far.

// Handling out_of_range and length_error exceptions.

#include <iostream>

#include <string>

using namespace std;

int main()

{

string sentence; //Line 1

string str1, str2, str3; //Line 2

const unsigned desired_length(26);

try //Line 3

{

sentence = "Testing string exceptions!"; //Line 4

cout << "Line 5: sentence = " << sentence

<< endl; //Line 5

cout << "Line 6: sentence.length() = "

<< static_cast<int>(sentence.length())

<< endl; //Line 6

str1 = sentence.substr(8, 20); //Line 7

cout << "Line 8: str1 = " << str1

<< endl; //Line 8

str2 = sentence.substr(28, 10); //Line 9

cout << "Line 10: str2 = " << str2

<< endl; //Line 10

str3 = "Exception handling. " + sentence; //Line 11

cout << "Line 12: str3 = " << str3

<< endl; //Line 12

str1.resize(str1.max_size() + 1, ' ');

}

catch (out_of_range re) //Line 13

{

cout << "Line 14: In the out_of_range "

<< "catch block: " << re.what()

<< endl; //Line 14

}

catch (length_error& le) //Line 15

{

cout << "Line 16: In the length_error "

<< "catch block: " << le.what()

<< endl; //Line 16

}

return 0; //Line 17

}

Explanation / Answer

Solution.cpp

//only error is length error and that is handled by length_error exceptin
#include <iostream>//input output function header file
#include <string>//this header file handle string functions
using namespace std;//it teells the cmpiler ti link std std namespace
int main()
{//main function
string sentence; //Line 1
string str1, str2, str3; //Line 2
const unsigned desired_length(26);//allocation length of the string 26
try //Line 3
{
sentence = "Testing string exceptions!"; //Line 4
cout << "Line 5: sentence = " << sentence
<< endl; //Line 5
cout << "Line 6: sentence.length() = "
<< static_cast<int>(sentence.length())// length of the string
<< endl; //Line 6
str1 = sentence.substr(8, 20); //Line 7//this function retrives part f the string
cout << "Line 8: str1 = " << str1
<< endl; //Line 8
str2 = sentence.substr(8, 10); //Line 9//8 after 8 position to 10 character it prints
cout << "Line 10: str2 = " << str2
<< endl; //Line 10
str3 = "Exception handling. " + sentence; //Line 11
cout << "Line 12: str3 = " << str3
<< endl; //Line 12
str1.resize(str1.max_size() + 1, ' ');//exception occurs at this statement because maxsize+1 we cannot add length to maxsize
}catch (out_of_range re) //Line 13
{
cout << "Line 14: In the out_of_range "//this statement will not called because error occurs is lenth_error
<< "catch block: " << re.what()
<< endl; //Line 14
}
catch (length_error& le) //Line 15
{
cout << "Line 16: In the length_error "//this statement will calls
<< "catch block: " << le.what()
<< endl; //Line 16
}

return 0; //Line 17

}

output

sh-4.3$ main                                                                                                                                                                    

Line 5: sentence = Testing string exceptions!                                                                                                                                   

Line 6: sentence.length() = 26                                                                                                                                                  

Line 8: str1 = string exceptions!                                                                                                                                               

Line 10: str2 = string exc                                                                                                                                                      

Line 12: str3 = Exception handling. Testing string exceptions!                                                                                                                  

Line 16: In the length_error catch block: basic_string::_M_replace_aux

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote