Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch substr exception?

I'm using substr for generating substring. How I can catch substr exception? For instance:

terminate called after throwing an instance of 'std::out_of_range'
like image 581
G-71 Avatar asked Jul 03 '26 13:07

G-71


2 Answers

Like this:

try
{
  /// use substr
}
catch( std::out_of_range& exception )
{
   // print out an error, and fail, or restart if appropriate.
}
like image 156
Scott Langham Avatar answered Jul 05 '26 03:07

Scott Langham


try
{
    std::string sub = mystring.substr(10,1);
}
catch (std::out_of_range & ex)
{
    cout << "caught exception!" << endl;
}
like image 25
Mark Ransom Avatar answered Jul 05 '26 02:07

Mark Ransom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!