Why " throw" will display a warning? - c++

#include <iostream>
#include <sstream>
using std::string;
using std::cout;
using std::endl;
using std::ostringstream;
class illegalParameterValue {
private:
string message;
public:
illegalParameterValue() : message("Illegal parameter value") {}
explicit illegalParameterValue(const char* theMessage) {message = theMessage;}
void outputMessage() {cout << message << endl;}
};
int main() {
ostringstream s;
s << "index = " << 1 << " size = " << 2;
throw illegalParameterValue(s.str().c_str());
return 0;
}
I just use some code like this, but the throw will remind some warnings which called
Clang-Tidy: Throwing an exception whose type 'illegalParameterValue' is not derived from 'std::exception'
How can I solve this problem?

consider the following code:
try {
// do something that might throw an exception
}
catch (const std::exception &ex) {
// handle the exception
}
If you derive your illegalParameterValue class from std::exception, then the catch clause above will catch it (along with other kinds of exceptions derived from std::exception). As currently written, it will not. Your users will have to add another catch clause:
try {
// do something that might throw an exception
}
catch (const std::exception &ex) {
// handle std exceptions
}
catch (const illegalParameterValue &ip) {
// handle illegal parameter exeception
}
Maybe that's what you want, maybe not.
But there's a lot of code out there like the first case.

Related

Exception not be caught by class and visual studio 2019 stopping at "Exception Unhandled"

I'm trying to create a class with a method to handle all exceptions. It accepts a pointer to a function, executes that function and then will handle if the function throws an exception. I then created a function get an invalid substring to prove the exception is caught, however, my class isn't getting the exception and Visual Studio is breaking on the substring line saying "Exception Unhandled"
cpp file:
#include "TestHarness.h"
using namespace TestHarness;
int testFunc();
int main()
{
Tester myTest;
std::cout << myTest.execute(testFunc);
return 0;
}
int testFunc()
{
std::cout << "My Test Func" << std::endl << std::endl;
std::string("abc").substr(10);
return 6;
}
h file:
#define TESTHARNESS_H
#include <vector>
#include <iostream>
#include <sstream>
#include <string>
namespace TestHarness
{
class Tester
{
public:
int execute(int(*func)());
private:
bool result;
};
int Tester::execute(int(*func)())
{
try {
(*func)();
result = true;
return 1;
}
catch (const std::runtime_error& e)
{
std::cout << e.what();
result = false;
return 0;
}
}
}
#endif
It's simple, really: substr throws std::out_of_range, which does not derive from std::runtime_error but from std::logic_error, itself derived from std::exception. Just catch std::exception const & or std::logic_error const &. Here is a list of inheritance for all standard exception types.

How to cast boost::exception?

Consider the following example (taken from https://theboostcpplibraries.com/boost.exception)
#include <boost/exception/all.hpp>
#include <exception>
#include <new>
#include <string>
#include <algorithm>
#include <limits>
#include <iostream>
typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info;
struct allocation_failed : public std::exception
{
const char *what() const noexcept { return "allocation failed"; }
};
char *allocate_memory(std::size_t size)
{
char *c = new (std::nothrow) char[size];
if (!c)
BOOST_THROW_EXCEPTION(allocation_failed{});
return c;
}
char *write_lots_of_zeros()
{
try
{
char *c = allocate_memory(std::numeric_limits<std::size_t>::max());
std::fill_n(c, std::numeric_limits<std::size_t>::max(), 0);
return c;
}
catch (boost::exception &e)
{
e << errmsg_info{"writing lots of zeros failed"};
throw;
}
}
int main()
{
try
{
char *c = write_lots_of_zeros();
delete[] c;
}
catch (boost::exception &e)
{
std::cerr << *boost::get_error_info<errmsg_info>(e);
}
}
The function allocate_memory() throws exception using the following statement
BOOST_THROW_EXCEPTION(allocation_failed{});
In the catch block how can I convert boost::exception &e back to allocation_failed?
Also, if my code has multiple throw statements like BOOST_THROW_EXCEPTION(A{}), BOOST_THROW_EXCEPTION(B{}), BOOST_THROW_EXCEPTION(C{}), etc. Where A, B, C are classes. Without using boost I can have separate catch blocks for each type of exception, in the following manner.
...
catch(A e){
...
}
catch(B e){
...
}
catch(C e){
...
}
How can I do the same when using boost, so that BOOST_THROW_EXCEPTION(A{}), BOOST_THROW_EXCEPTION(B{}), BOOST_THROW_EXCEPTION(C{}), etc. go to different catch blocks?
I am new to boost library, and some of its concepts elude me.
BOOST_THROW_EXCEPTION always throws a type which inherits the type of its argument, in addition to inheriting boost::exception. This means two things:
You can dynamic_cast from boost::exception to the type that was passed.
-
catch (boost::exception &e)
{
std::cerr << *boost::get_error_info<errmsg_info>(e);
if ( allocation_failed* af = dynamic_cast<allocation_failed*>(&e) )
{
std::cerr << af->what() << std::endl; // redundant
}
}
You can directly catch the type that was passed. However, you should always catch by reference to avoid slicing. (This advice is independent of whether you're using boost or whatever.)
-
catch (A& a) {
// ...
} catch (B& b) {
// ...
} catch (C& c) {
// ...
}
Of course, doing it this way, if you want any of boost's error formatting or extra data, you would need to then dynamic_cast the exception object to boost::exception*.

Why is the destructor of an exception called twice?

I have following program:
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;
class MyError : public runtime_error
{
public:
MyError(string mess = "");
~MyError(void);
};
MyError::MyError(string mess) : runtime_error(mess)
{
cout << "MyError::MyError()\n";
}
MyError::~MyError(void)
{
cout << "MyError::~MyError\n";
}
int main(void)
{
try {
throw MyError("hi");
}
catch (MyError& exc) {
cout << exc.what() << endl;
}
cout << "goodbye\n";
return 0;
}
Which prints the following:
MyError::MyError()
MyError::~MyError
hi
MyError::~MyError
goodbye
Why is the destructor of the exception (~MyError()) called twice?
I assumed that throw creates a new object, but I do not understand why the class destructor is called.
If you instrument the exception's copy or move constructor, you'll find it's called once before the handler. There's a temporary exception object into which the thrown expression is copied/moved, and it is this exception object to which the reference in the handler will bind. C++14 15.1/3+
So the execution resulting from your code looks something like this (pseudo-C++):
// throw MyError("hi"); expands to:
auto tmp1 = MyError("hi");
auto exceptionObject = std::move(tmp1);
tmp1.~MyError();
goto catch;
// catch expands to:
MyError& exc = exceptionObject;
cout << exc.what() << endl;
// } of catch handler expands to:
exceptionObject.~MyError();
// normal code follows:
cout << "goodbye\n";
Because your compiler is failing to elide the copy from the temporary to the exception object managed by the exception handling mechanism.
Conceptually, MyError("hi") creates a temporary, which will be destroyed at the end of the statement (before the exception can be handled). throw copies the thrown value somewhere else, where it will persist until after the exception has been handled. If the thrown value is a temporary, then a decent compiler should elide the copy and initialise the thrown value directly; apparently, your compiler didn't do that.
My compiler (GCC 4.8.1) does a rather better job:
MyError::MyError()
hi
MyError::~MyError
goodbye
Your exception is being copied. If you instrument the copy ctor, you can see this:
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;
class MyError : public runtime_error
{
public:
MyError(MyError const &e) : runtime_error("copy") { std::cout << "Copy MyError"; }
MyError(string mess = "");
~MyError(void);
};
MyError::MyError(string mess) : runtime_error(mess) {
cout << "MyError::MyError()\n";
}
MyError::~MyError(void) {
cout << "MyError::~MyError\n";
}
int main(void) {
try {
throw MyError("hi");
}
catch (MyError& exc) {
cout << exc.what() << endl;
}
cout << "goodbye\n";
return 0;
}
Result:
MyError::MyError()
Copy MyError
MyError::~MyError
copy.what()
MyError::~MyError
goodbye

What() method for std::exception isn't acting virtual?

So in the reference manual, the what() method is described as virtual, but it doesn't seem to be acting that way. (I am compiling with g++ and the c++11 flag)
#include <stdio.h> //printf
#include <iostream> //cout
#include <stdexcept>// std::invalid_argument
using namespace std;
void fn(){
throw runtime_error("wowwowo");
}
int main(){
try {fn(); }
catch(exception a) {cout << a.what() << endl;}
return 0;
}
The output for this is "std::exception", as opposed to the error message, "wowwowo". However, if I change the catch type to runtime_error, it behaves as expected. I have some code where I would like to catch exceptions that may or may not be runtime_errors, and I suppose I could have multiple catch blocks, but I'm curious as to why the code behaves as it does. Here's the code that prints out the error message:
#include <stdio.h> //printf
#include <iostream> //cout
#include <stdexcept>// std::invalid_argument
using namespace std;
void fn(){
throw runtime_error("wowwowo");
}
int main(){
try {fn(); }
catch(runtime_error a) {cout << a.what() << endl;}
return 0;
}
Change this statement:
catch(exception a) {cout << a.what() << endl;}
to this:
catch(const exception &a) {cout << a.what() << endl;}
You have to catch an exception by reference in order for it to use polymorphism. Otherwise, you are slicing the std::runtime_error object so only a std::exception object remains, thus std::exception::what() will be called instead of std::runtime_error::what().
As for the function itself, it is indeed a virtual function.
class exception {
public:
//...
virtual const char* what() const noexcept;
};

Printing name of the initializing function for a class

Is it possible to capture and print name of the function which initialized a class object? What I want is something like this:
class MyException: public std::exception
{
public:
MyException(const std::string message, const std::string caller = __func__)
: _caller(caller),
_msg(message)
{
...
}
std::string what(void); // prints messaged along with the caller
private:
...
}
In the above class MyException I want the caller to capture the function name where an object was instantiated so that user is warned of which function threw. I can always do
...
throw MyException(message, __func__);
...
while removing the default __func__. But then I am doing this trivial thing every time I instantiate an MyException object.
Is there a better way to instruct user exactly which function throws?
Thanks in advance,
Nikhil
If I wanted to provide automatic context for my exceptions I'd do it like this:
#include <iostream>
#include <stdexcept>
#include <string>
struct context_info {
explicit context_info(std::string function)
: _function { std::move(function) }
{}
// perhaps the context info will increase later to include class and instance?
friend std::string to_string(const context_info& ci)
{
return ci._function;
}
private:
std::string _function;
};
struct my_exception : std::runtime_error
{
my_exception(const std::string& message)
: std::runtime_error(message)
{}
my_exception(context_info context, const std::string& message)
: my_exception(message + " called from " + to_string(context))
{}
// everything else already taken care of for you
};
#if defined(NDEBUG)
#define THROW_WITH_CONTEXT(TYPE, ...) throw TYPE(__VA_ARGS__)
#else
#define THROW_WITH_CONTEXT(TYPE, ...) throw TYPE(context_info(__func__), __VA_ARGS__)
#endif
using namespace std;
void do_foo()
try {
THROW_WITH_CONTEXT(my_exception, "foo failure");
}
catch(const exception& e) {
cout << e.what() << endl;
}
int main() {
// your code goes here
try {
do_foo();
THROW_WITH_CONTEXT(my_exception, "error");
}
catch(const exception& e) {
cout << e.what() << endl;
}
return 0;
}
expected output:
foo failure called from do_foo
error called from main
However, neat as this is it's probably not what you should really be doing as it won't provide you with enough context in a large complex program.
have a look at the documentation for std::throw_with_nested etc. You can catch an exception at each stage of the call stack and re-throw it with additional context information. This is much more useful.
http://en.cppreference.com/w/cpp/error/throw_with_nested