When a function is called, the arguments that are passed into that function are evaluated. If there are too many or too few arguments, the evaluation can fail.

If there is an argument that is not a valid type, the evaluation can fail. If an argument is not provided, the evaluation can fail.

These failures can cause crashes and serious bugs in code. Debugging these problems can be very difficult and time-consuming.

STL exceptions are a way of reporting errors in the Standard Template Library (STL). Exceptions are part of the C++ language, so they do not depend on any other libraries. Errors caused by invalid arguments or non-existent arguments can be reported with exceptions.

This article will discuss how to throw and terminate calls to functions after throwing an instance of STL::invalid_argument what(): Stoi().

What is this error and why did I get it?

terminate called after throwing an instance of 'std::invalid_argument' what(): stoi

This error occurs when you attempt to call the standard C library function std::exit . The std::exit function takes no arguments and immediately calls the C library function system exit , which terminates the process.

When you attempt to call std::exit , if you provide it with an invalid argument, such as a null argument or an argument of the wrong type, then it will throw an instance of Stoiinvalid_argument what() .

What this means is that it detects that you attempted to pass it an argument of some type, but that argument was not valid. For example, if you tried to pass it a null argument, then it would be detecting that you attempted to pass it an argument that is not something-something else. Something-something else being anything else besides null.

The problem

When you try to assign a value to a variable that is out of range, like assigning a negative number to a variable meant to hold only positive numbers, the compiler can’t know that the value is invalid.

Since it can’t know, it will happily compile the code that tries to assign the invalid value, which can lead to unexpected crashes at runtime.

The compiler could perhaps eliminate the instance of std::invalid_argument it generates in this case, but only at the cost of generating more instances of other exceptions that are just as hard to diagnose and avoid.

This would not be an acceptable solution because it would not prevent unexpected crashes due to invalid values at runtime.

Furthermore, eliminating instances of std::invalid_argument at compile time would not address the root cause of the problem: attempting to assign an invalid value to a variable.

The solution

terminate called after throwing an instance of 'std::invalid_argument' what(): stoi

The solution to this problem is very simple. Just call the std::exit() function in the same line as the throw declaration.

Using our previous example, this would look like:

void someFunction() { if (condition) { throw SomeException(); } // …more code here }

By placing the exit() function at the end of the throw SomeException(); declaration, you are telling the compiler that there is no more code to execute after that, so it should quit right then and there.

Simple as that! You have solved the problem of crashing your program. Now your user can continue using your software without any problems.

Remember: Whenever you throw an exception, always place a call to std::exit() inside of the same line as the throw statement. That way, you can be sure that your program will not stay stagnant and continue to crash.

Understanding the solution

terminate called after throwing an instance of 'std::invalid_argument' what(): stoi

The solution to this problem is to properly handle the error that is being thrown. By passing a special type of parameter to the function, we can tell the function how to respond to an error.

Function parameters are named values that are passed into a function. A function can have multiple parameters, and some parameters can be required and others optional.

In order to handle the exception being thrown, we need to pass a special kind of parameter into the constructor. This parameter is called a std::unique_ptr<:string> invalid_argument_option().

What this does is it takes away the possibility of creating an instance of string which is invalid. Since we know that there is an issue with the string being passed in, this parameter removes that issue.

To solve this problem, we need to first check if the string is valid or not before passing it into the constructor.

Solving the problem

terminate called after throwing an instance of 'std::invalid_argument' what(): stoi

Now that we know what the problem is, how do we solve it? The solution is to replace all instances of std::string with a stronger type of string: either std::string_view or char* .

For strings that are constructed via std::string , the simplest fix is to replace std::string with char* . This way, you avoid implicit conversions and guarantee that the string can be correctly handled by IOS and Android.

For strings that are passed in from external sources (such as a user input or a file read), you can use string_view instead. String_view is a strong semantic-aware type that acts like a reference to a string literal. This way, you can ensure that the string is not modified after initialization.

You can test your app using both of these types to ensure that all instances of std::string are replaced with stronger types.

Using catch() with conditional operators

terminate called after throwing an instance of 'std::invalid_argument' what(): stoi

When writing a catch block, it is possible to use a conditional operator to determine whether or not to execute the catch block. This is done by checking the std::exception type that was thrown.

If the exception type is not one of your exception types listed in the catch statement, then the code inside the catch block is not executed. This can be used in situations where you know what exception was thrown and can handle it, so you do not want to handle any other exceptions.

This is very useful as it prevents unnecessary handling of exceptions that were not produced by your code. By using only exceptions that are specific to your code, you are able to more effectively solve the problem at hand.

When using conditional operators in catches, it is important to remember that true equals true, but false does not equal false.

Using a unique_ptr with terminate()

terminate called after throwing an instance of 'std::invalid_argument' what(): stoi

Another way to use the unique_ptr is to pass it a function that will delete the object it points to. This can be done in two ways: with a lambda function or with an instance of std::destroy().

The first way is with a lambda function. A lambda function is just a functional parameter that can be passed directly to terminate(). The advantage of using a lambda is that it can be used for other things than just destroying an object, so it can have additional functionality.

The second way is with an instance of std::destroy(). This must be an instance of std::destroy(), not a unique_ptr<:destroy>, so make sure you check that before applying this method!

Both of these ways invalidate the use of reset(), so make sure you only apply these after terminating the object with them.

Understanding the solution

terminate called after throwing an instance of 'std::invalid_argument' what(): stoi

The solution to this problem is to explicitly tell the compiler that the argument is not being used. This is done by passing a special type called void .

By declaring the argument as void , the compiler knows that the argument is not being used and will not throw another compilation error.

std::vector v(10); std::sort(v.begin(), v.end(), [](int lhs, int rhs) { return lhs

In this case, the second parameter of the generic lambda is declared as int , which is a scalar type. Since sort() needs a comparison function that takes two arguments of any type, it can use the generic lambda expression despite only having one parameter.

The compiler implicitly declares the other parameter of the comparison function as void because there is no other valid type for it to be.

LEAVE A REPLY

Please enter your comment!
Please enter your name here